1

我对如何编写以下查询有点困惑:

我有两张桌子

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_two 中但针对特定用户的 tbl_one 行。

例如通过下面的选择:

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
4

5 回答 5

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
于 2012-11-02T09:49:08.863 回答
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
于 2012-11-02T12:49:33.803 回答
1
select * 
from tbl_one 
where ID not in (select tblone_ID 
                 from tbl_two 
                 where userID = 50) 
于 2012-11-02T09:43:36.900 回答
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
于 2012-11-02T09:44:55.897 回答
0

你可以试试这个

select * from tbl_one t1 
where t1.id not in (select tblone_id from tbl_two where tbl_two.userid=50)
于 2012-11-02T09:45:31.743 回答