简体   繁体   中英

Trying to return values in Table A that do not exist in Table B

I have two tables for inventory tracking. TABLE_A is for the actual records, TABLE_B is for that record's history with regards to it's location; moved from Building 1 to Building 2, etc.

Most of the records in TABLE_A are in TABLE_B, but there are a few that are in TABLE_A that are not in TABLE_B. I'm trying to write a query to only get which records in TABLE_A do not exist in TABLE_B.

TABLE_A.Item_ID
101
102
103
104
105

TABLE_B.Item_ID
101
103
104

I thought doing the following would work, but it's not.

select a.Item_ID
from TABLE_A a 
left outer join TABLE_B b on (a.Item_ID = b.Item_ID)
where b.Item_ID is null
order by a.Item_ID desc 

Thanks in advance for your help.

I'd try the MINUS set operator:

select item_id from table_a
minus
select item_id from table_b

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-2024 STACKOOM.COM