我有兩個表稱爲用戶&連接SQL查詢來獲取未連接的用戶列表
我需要獲取用戶列表怎麼沒有從用戶表連接
如用戶ID 1已經發送的請求&連接到4 & 6.如何編寫一個查詢來獲取其他未與userid(1)連接的用戶。
預期結果將是用戶id = 1
2 bb [email protected]
3 cc [email protected]
5 ff
答案被理解。
我有兩個表稱爲用戶&連接SQL查詢來獲取未連接的用戶列表
我需要獲取用戶列表怎麼沒有從用戶表連接
如用戶ID 1已經發送的請求&連接到4 & 6.如何編寫一個查詢來獲取其他未與userid(1)連接的用戶。
預期結果將是用戶id = 1
2 bb [email protected]
3 cc [email protected]
5 ff
答案被理解。
簡單woulb是:
SELECT * from users a
WHERE userid != :userid
AND not exists (select * from connections
where (userid = :userid and c_userid = a.userid)
or (userid = a.userid and c_userid = :userid))
下面將退回沒有在連接表中的所有用戶。
SELECT * FROM users AS u
WHERE (SELECT count(*) FROM connections AS c
WHERE c.userid=u.userid AND reqType != 0) = 0;
下面的查詢獲取用戶不是與用戶ID相關聯= 1
select u.userid,u.name,u.email
from connections
left join users as u on connections.c_userid = u.userid
where connections.userid =1 and connections.reqtype in (0,1)
您嘗試此查詢
select *from users where userid not in (select c_userId from connenctions where where userid=1) and userid!=1
這個作品般的魅力 – Anand