简体   繁体   中英

writing an sql query?

I'm having 2 tables

table Items Table (this table holds all items I'm having)

itemId
---------
Item1
Item2
Item3
Item4
Item5

table 2 users_item relation 
UserId  || ItemId
1  || Item1
1  || Item2

userId one has stored 2 items Item1,Item2. Now I want to write a query on table1 (Items table) so that it displays all items which user1 has NOT chosen.

select itemid from Items where itemid not in 
(select itemid from users_item where userid = 1)
select itemid 
from Items i
left join (select ItemId from users_item where userId = 1)chosen on chosen.ItemId = i.itemId
where chosen.ItemId is null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM