简体   繁体   English

IN / ALL / ANY子查询中的MySQL列'id'不明确

[英]MySQL Column 'id' in IN/ALL/ANY subquery is ambiguous

I am trying to do a search functionalities that involves three tables. 我正在尝试进行涉及三个表的搜索功能。

Searching for users and returning wheather the user id 1 is a friend of the returned users. 搜索用户并返回用户ID 1的用户是返回用户的朋友。 Also The returned users is being filtered from a third table where it checks tag of that users. 还从第三张表中过滤返回的用户,在该表中检查该用户的标记。

So I can say, "Return users who has tag 'Programming', 'Php' in userinterests table and also if the returned user is a friend of usr id 1 or not " 因此,我可以说:“返回在userinterests table具有标签'Programming', 'Php'的用户,以及返回的用户是否是usr id 1的朋友”

I am trying to use the bellow query but getting Column 'id' in IN/ALL/ANY subquery is ambiguous If I remove the left join then it works. 我正在尝试使用波纹管查询,但Column 'id' in IN/ALL/ANY subquery is ambiguous查询中获取Column 'id' in IN/ALL/ANY subquery is ambiguous

SELECT n.id, n.firstName, n.lastName, t.id, t.tag, t.user_id, if(id in (
        SELECT u.id as id from friends f, users u 
        WHERE CASE 
        WHEN f.following_id=1
        THEN f.follower_id = u.id 
        WHEN f.follower_id=1
        THEN f.following_id = u.id
        END 
        AND
        f.status= 2
    ), "Yes", "No") as isFriend 
FROM users n
LEFT JOIN userinterests t on  n.id = t.id

WHERE t.tag in ('Programming', 'Php')

Thank you for your time :) 感谢您的时间 :)

Qualify all your column names. 限定所有列名。 You seem to know this, because all other column names are qualified. 您似乎知道这一点,因为所有其他列名都是合格的。

I'm not sure if your logic is correct, but you can fix the error by qualifying the column name: 我不确定您的逻辑是否正确,但是您可以通过限定列名来解决错误:

SELECT . . . 
       (CASE WHEN n.id IN (SELECT u.id as id 
                           FROM friends f CROSS JOIN
                                users u 
                           WHERE CASE WHEN f.following_id=1
                                      THEN f.follower_id = u.id 
                                      WHEN f.follower_id=1
                                      THEN f.following_id = u.id
                                 END 
                           ) AND
                 f.status= 2
             THEN 'Yes' ELSE 'No'
        END) as isFriend 
. . . 

This is the way I will go for your approach: 这就是我将采用的方法:

1) I used INNER JOIN instead of LEFT JOIN for skip users that are not related to tags: Programming and Php . 1)对于与标签无关的跳过用户,我使用INNER JOIN而不是LEFT JOINProgrammingPhp

2) I replaced the logic to find the set of friends related to user with id equal to 1 . 2)我替换了逻辑以查找与id等于1用户相关的朋友集合。

SELECT
    n.id,
    n.firstName,
    n.lastName,
    t.id,
    t.tag,
    t.user_id,
    IF(
        n.id IN (SELECT follower_id FROM friends WHERE status = 2 AND following_id = 1
                 UNION
                 SELECT following_id FROM friends WHERE status = 2 AND follower_id = 1),
        "Yes",
        "No"
    ) AS isFriend
FROM
    users n
INNER JOIN
    userinterests t ON n.id = t.id AND t.tag IN ('Programming', 'Php')

Just curious, whats is the meaning of status = 2 ? 只是好奇, status = 2的含义是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM