[英]Joining two tables with conditon
I have two tables: 我有两个表:
users
- id
- fname
- lname
- is_online
and: 和:
friendships
- friendA (references users)
- friendB (references users)
- status (1 means they are friends)
I want to query all friends of a given user who are online. 我想查询给定用户在线的所有朋友。
SELECT
t1.*
FROM users t1
WHERE
t1.id IN (
SELECT
friendB
FROM friendships
WHERE
friendA = CURRENT_USER_ID
UNION
SELECT
friendA
FROM friendships
WHERE
friendB = CURRENT_USER_ID
)
t1.is_online = 1
AND NOT t1.id = CURRENT_USER_ID
Select
Unique(id)
from
users outer join
friendships where
((friendA = id and friendB = ID_TO_QUERY) or
(friendA = ID_TO_QUERY and friendB = id) and status = 1)
and is_online = true
and id /= ID_TO_QUERY
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.