0

这是查询中使用的表:

帮助(相关字段: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

但这显然超出了我的知识范围。任何人都可以告诉我灯光吗?

4

1 回答 1

1

如果我没记错的话,你需要指定ID它来自哪里,因为所有表都有 column ID

尝试添加 tableName Helps,因为它包含在子查询中x

所以在你的查询中,

SELECT x.ID, ....
FROM...
WHERE ....
于 2012-10-10T16:35:29.823 回答