简单的SQL问题(mysql)

时间:2009-09-28 19:22:32

标签: sql mysql

SELECT username, (SELECT follow 
                  FROM follow 
                  WHERE follow_user_id = user_id) AS following
FROM user
WHERE user_id = 1

我想知道如何检查是否跟随(子查询(选择跟随...))返回一个值。如果是,请将其替换为“是”。如果没有,则将其替换为“否”。

2 个答案:

答案 0 :(得分:7)

使用案例陈述

select username, 
  Case When Exists 
      (select * from follow 
       where follow_user_id = user_id)
    Then 'Yes' Else 'No' End following
from user
where user_id = 1

答案 1 :(得分:0)

SELECT u.username,IF((SELECT COUNT(*)FROM follow f WHERE f.follow_user_id = u.user_id),“yes”,“no”)FROM user u WHERE u.user_id = 1