我对如何编写以下查询感到有点困惑:
我有两张桌子
tbl_one
ID TEXT
1 test1
2 test2
3 test3
4 test4
5 test5
和
tbl_two
ID userID tblone_ID
1 50 1
2 100 1
3 100 2
我希望获得tbl_one的行,这些行不会出现在tbl_two中,而是针对特定用户。
例如通过以下选择:
select * from tbl_one, tbl_two
where (tbl_two.tblone_ID !=tbl_one.ID and tbl_two.userID==50)
我的愿望将是
Desire resalt:
ID TEXT
2 test2
3 test3
4 test4
5 test5
答案 0 :(得分:2)
SELECT a.*
FROM tableA a
LEFT JOIN tableB b
on a.ID = b.tblone_ID AND
b.userID = 50
WHERE b.tblone_ID IS NULL
答案 1 :(得分:1)
select *
from tbl_one
where ID not in (select tblone_ID
from tbl_two
where userID = 50)
答案 2 :(得分:1)
select * from tbl_one where tbl_one.ID not in ( Select tblone_ID FRom tbl_two where userID=50) SELECT * from tbl_one inner Join tbl_two on tbl_one.ID !=tblone_ID where userID=50
答案 3 :(得分:0)
SELECT * FROM tbl_one
LEFT JOIN tbl_two on tbl_one.ID = tbl_two.tblone_ID
WHERE tbl_one.ID IS NULL AND tbl_two.userID = 50
答案 4 :(得分:0)
你可以试试这个
select * from tbl_one t1
where t1.id not in (select tblone_id from tbl_two where tbl_two.userid=50)