我有兩個表:連接兩個表與conditon
users
- id
- fname
- lname
- is_online
和:
friendships
- friendA (references users)
- friendB (references users)
- status (1 means they are friends)
我想查詢一個給定的用戶誰是網上的所有朋友。
我有兩個表:連接兩個表與conditon
users
- id
- fname
- lname
- is_online
和:
friendships
- friendA (references users)
- friendB (references users)
- status (1 means they are friends)
我想查詢一個給定的用戶誰是網上的所有朋友。
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
請仔細地縮進您的查詢,並使用代碼格式。 – Barmar
樣本數據和預期的結果真正幫助傳達你想要做什麼。如果你正在學習SQL,你應該包括你試圖回答這個問題。 –
嘗試INNER JOIN查詢 –
SELECT T1.fanme,T2freindA,FROM Table1 T1 INNER JOIN Table2 T2 ON T1.id = T2.id WHERE T1.IS_ONLINE = 1 –