向结果集添加新的行集到现有查询

时间:2012-10-10 16:32:05

标签: mysql sql join sql-order-by

这是查询中使用的表格:

HELPS(相关字段:id,id_user)

enter image description here

朋友

enter image description here

SHARED_HELPS

enter image description here

所以:

此查询(有效)返回来自用户的帮助以及他引用的其他用户的所有帮助:

$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

但这显然是出于我的知识..有人能告诉我这个吗?

1 个答案:

答案 0 :(得分:1)

如果我没有弄错,您需要指定列ID来自何处,因为所有表都有列ID

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

在你的查询中,

SELECT x.ID, ....
FROM...
WHERE ....