如果不匹配,则访问查询默认值

时间:2017-04-04 07:06:57

标签: sql ms-access select join

如何在Microsoft Access中制定查询以从这两个源表生成下面的结果表?

表1

Item Code
---------
Item 01
Item 02
Item 03

表2

Item Code | Quotation
----------+----------
Default   |      5000
Item 01   |      1000
Item 02   |      2000

我想要结果表必须是这样的:

结果表

Item Code | Quotation
----------+----------
Item 01   |      1000
Item 02   |      2000
Item 03   |      5000 <== note no matching item code in table 2

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT T1.No, T1.ItemCode, T2.Quotation
FROM TABLE01 T1 
INNER JOIN TABLE02 T2 
        ON T1.ItemCode = T2.ItemCode 
        OR (T1.ItemCode <> T2.ItemCode AND T2.ItemCode = 'Default');

答案 1 :(得分:0)

我认为最好将默认值存储在专用表中,而不是必须将项目移出&#39;其他值:

SELECT t1.ItemCode, t2.Quotation
  FROM Table01 t1,  Table02 t2
  WHERE t1.ItemCode = t2.ItemCode
UNION
SELECT t1.ItemCode, t2.Quotation
  FROM Table01 t1, Table02 t2
 WHERE t2.ItemCode = 'Default'
       AND t1.ItemCode NOT IN ( SELECT ItemCode FROM Table02 );