Reputation: 1427
I'm trying to write a query that returns true if the same idx exists in another table.
Here is what I want to do.
Two Tables:
User (user_idx, name)
Group (group_idx, user_idx)
Pseudo Query:
SELECT user_idx, name, (True(1)/False(0) value) as has_joined_group
FROM User
WHERE (Check if user_idx exists in Group table where group_idx is 3)
Can this be done using Mysql? If yes, how?
Upvotes: 3
Views: 1924
Reputation: 1549
SELECT u.user_idx, u.name, if(g.user_idx IS NOT NULL,'true','false') as has_joined_group
FROM User AS u
LEFT JOIN Group AS g
ON g.user_idx = u.user_idx AND g.group_idx = 3
Upvotes: 1
Reputation: 44601
SELECT
u.user_idx,
u.name,
CASE WHEN g.user_idx IS NOT NULL AND g.group_idx = 3 THEN 1 ELSE 0 END AS has_joined_group
FROM
user u JOIN LEFT group g ON u.user_idx = g.user_idx
Upvotes: 1
Reputation: 782389
SELECT u.user_idx, u.name, g.user_idx IS NOT NULL AS has_joined_group
FROM User AS u
LEFT JOIN Group AS g
ON g.user_idx = u.user_idx AND g.group_idx = 3
Upvotes: 3