Basit
Basit

Reputation: 17184

Simple SQL problem (mysql)

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

I want to know how I can check if follow (sub-query (select follow ...)) returns a value. If it did, then replace it with 'yes'. If it didn't, then replace it with 'no'.

Upvotes: 1

Views: 155

Answers (2)

Johan Soderberg
Johan Soderberg

Reputation: 2740

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

Upvotes: 0

Charles Bretana
Charles Bretana

Reputation: 146499

Use a case statement

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

Upvotes: 7

Related Questions