这是查询中使用的表:
帮助(相关字段:id、id_user)
朋友们
SHARED_HELPS
所以:
此查询(有效)返回用户的所有帮助以及他引用的其他用户的所有帮助:
$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';
此外,此查询(有效)列出了用户的所有帮助以及他的朋友的帮助
$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
';
问题是最后一个,我需要添加来自其他用户但他引用的那些(就像我发布的两个查询的混合......)
我试过这个:
$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
';
但我得到了:
Column 'id' in field list is ambiguous
但这显然超出了我的知识范围。任何人都可以告诉我灯光吗?