繁体   English   中英

MySQL查询基于另一个表的条件

[英]MySQL query based on condition from another table

我正在尝试编写一个MySQL查询,该查询根据另一个表中评估的条件选择10个用户名。

结果将是10个用户名作为建议。 因此,我需要选择登录用户当前未遵循的10个用户名。

以下内容返回了已经关注的用户,因此有问题。 任何想法如何解决?

"SELECT  username
FROM    users
WHERE   NOT EXISTS
        (
        SELECT  id 
        FROM    user_followers
        WHERE   user_followers.user_followed_id = users.username AND user_followers.user_follower_id = ?
        )  
ORDER BY followers DESC LIMIT 10 "

user_followed_id-正在外部查询中评估的用户的用户名。
user_follower_id-对其进行检查的用户的用户名(使用准备好的语句)

也许尝试左加入

  SELECT *
  FROM users  u
  LEFT JOIN user_followers uf
    ON u.username  = uf.user_followed_id 
   AND uf.user_follower_id <> ?

我希望这有帮助:

SELECT  username
FROM    users
WHERE username  NOT EXISTS
    (
    SELECT  user_followers.user_followed_id -- I see in you code below that you use this to store the username 
    FROM    user_followers
    WHERE   user_followers.user_follower_id = ?
    )
AND username <> ?  -- if the parameter is not the username,may be changed by the id column name of the user
ORDER BY followers DESC LIMIT 10 

暂无
暂无

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

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