This are the tables that are used in the querys:
HELPS (Fields relevant: id, id_user)

FRIENDS

SHARED_HELPS

So:
This query (works) returns al the helps from a user and all from other users that he referenced:
$sql = 'SELECT
helps.*,CASE WHEN shared_helps.userid IS NULL THEN 0 ELSE shared_helps.userid END as is_shared, CASE WHEN shared_helps.userid IS NULL THEN helps.fecha ELSE shared_helps.fecha END as ffecha
FROM
helps
LEFT JOIN shared_helps
ON shared_helps.helpid = helps.id
AND shared_helps.userid = '.$value.'
WHERE (helps.id_user = '.$value.' AND helps.id_group <= 0) OR shared_helps.userid = '.$value.'
ORDER BY ffecha DESC';
Also, this query (works) lists all the helps from a user an the ones from his friends
$sql = 'SELECT id, title, content, id_user, id_group, id_type, id_loc, avatar, attached, fecha, likes, lan, needsCount, recivedCount
FROM
(
SELECT *, 1 as OrderBy
FROM helps
WHERE id_user = '.$value.' or id_user IN (SELECT helpid FROM shared_helps WHERE userid = '.$value.')
UNION
SELECT h.*, 2 as OrderBy
FROM (
SELECT id AS friendsId,
CASE followerid
WHEN '.$value.' THEN followingid
ELSE followerid
END AS friend_id
FROM friends
WHERE acepted = 1 AND
(followerid ='.$value.' OR followingid = '.$value.')
) AS f
INNER JOIN helps AS h
ON h.id_user = f.friend_id where id_group < 0
) x
ORDER BY ID DESC
';
The thing is to the last one, i need to add the ones that are from other users but he referenced (is like a mix of the two query I posted..)
I tried this:
$sql = 'SELECT id, title, content, id_user, id_group, id_type, id_loc, avatar, attached, fecha, likes, lan, needsCount, recivedCount,
CASE WHEN shared_helps.userid IS NULL THEN helps.fecha ELSE shared_helps.fecha END as ffecha
FROM
(
SELECT *, 1 as OrderBy
FROM helps
WHERE id_user = '.$value.' or id_user IN (SELECT helpid FROM shared_helps WHERE userid = '.$value.')
UNION
SELECT h.*, 2 as OrderBy
FROM (
SELECT id AS friendsId,
CASE followerid
WHEN '.$value.' THEN followingid
ELSE followerid
END AS friend_id
FROM friends
WHERE acepted = 1 AND
(followerid ='.$value.' OR followingid = '.$value.')
) AS f
INNER JOIN helps AS h
ON h.id_user = f.friend_id where id_group < 0
) x
LEFT JOIN shared_helps
ON shared_helps.helpid = x.id
AND shared_helps.userid = '.$value.'
WHERE (x.id_user = '.$value.' AND helps.id_group <= 0) OR shared_helps.userid = '.$value.'
ORDER BY ffecha DESC
';
But i got:
Column 'id' in field list is ambiguous
But this is obviously out of my knowledge.. Can anyone show me the ligth?
if I’m not mistaken, you need to specify where column
IDdoes it come from because all tables has columnID.Try adding the tableName
Helpsand since it contains in a subqueryxSO in your query,