最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - Get some records from one table then get records that don't match those from another table - Stack Overflow

programmeradmin2浏览0评论

I have 2 tables I need information from. "Jobs" and "tempItems".

Jobs table:

JobID Item QTY
0007 Test 1 1
0007 Test 2 1

I have 2 tables I need information from. "Jobs" and "tempItems".

Jobs table:

JobID Item QTY
0007 Test 1 1
0007 Test 2 1

tempItems:

Item QTY
Test 1 0
Test 2 0
Test 3 0
Test 4 0

I need results from the Jobs table AND the tempItems that isn't in the Jobs table

Item QTY (explanation)
Test 1 1 <-- from jobs table
Test 2 1 <-- from jobs table
Test 3 0 <-- from tempItems table
Test 4 0 <-- from tempItems table

I just can't seem to wrap my head around it.

Share Improve this question edited Mar 31 at 19:48 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 31 at 11:00 Bill EBill E 11 silver badge New contributor Bill E is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • 1 You can't having done much research on this. A left outer join between Item will do - Access even has a wizard that will help you to achieve this. – Gustav Commented Mar 31 at 11:24
Add a comment  | 

2 Answers 2

Reset to default 1

As @Gustav said - the wizard will help you create an "Unmatched Query" to return everything in tempItems that isn't in Jobs:

SELECT tempItems.Item, tempItems.Qty
FROM tempItems LEFT JOIN Jobs ON tempItems.[Item] = Jobs.[Item]
WHERE (((Jobs.Item) Is Null));

Then all you need to do is Union that with a query returning everything from the Jobs table (and remove all the brackets - Access just loves brackets).

SELECT  Item, 
        Qty
FROM    Jobs

UNION SELECT tempItems.Item, 
             tempItems.Qty
FROM tempItems LEFT JOIN Jobs ON tempItems.Item = Jobs.Item
WHERE Jobs.Item Is Null

There's no need to name the table for each field in the first query - Jobs is the only table being used.

The field names in the second (unioned) table need the table name as Access won't know if you mean the field in tempItems or Jobs.

I've removed all the brackets (square brackets are only need around fields with a space in).

Try construct query

SELECT iif(tempItems.Item is not null,tempItems.Item,Jobs.Item)  as [Item], 
   nz(nz(Jobs.QTY,tempItems.QTY),0)
FROM tempItems LEFT JOIN Jobs  ON tempItems.Item = Jobs.Item;

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论