我有一個如下表
表名:用戶

表名:專案

表名:user_project

所以我想獲得不屬于專案“1”的用戶。
我試過這樣但沒有運氣
SELECT u.id, u.username
FROM testdb.user_project up
LEFT JOIN testdb.users u
ON up.userId = u.id
WHERE up.projectId=1 AND u.id IS NULL
我的預期輸出
id | username
--------------
3 | u3
4 | u4
你能幫我解決這個問題嗎
先感謝您
uj5u.com熱心網友回復:
下面的查詢應該作業
select u.*
from users u
where u.id not in (select userId from user_project where projectId = 1)
uj5u.com熱心網友回復:
使用不存在
select u.* from users u
where not exists ( select 1 from user_project up
where up.userid= u.id and up.projectid=1)
uj5u.com熱心網友回復:
基本上,您希望關系表中沒有與 Project 1 關聯的用戶。您可以使用“不存在的地方”來做到這一點。
解決方案更好,因為存在消除了處理重復值的需要,因為一次出現足以匹配(或不匹配)定義。
select
u.id
, u.username
from users u
where not exists (
select
1
from user_project up
where 1=1
and up.projectID = 1
and up.userId = u.id
)
uj5u.com熱心網友回復:
SELECT u.id, u.username
FROM testdb.user_project up
LEFT JOIN testdb.users u
ON up.userId = u.id
WHERE up.projectId <> 1;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/425012.html
