如何从表A中查找表B中没有的记录?

时间:2014-02-26 19:09:49

标签: sql database sql-server-2008 tsql

我使用的是SQL Server 2008数据库。

我有两个包含这些列的表:

Table A

request_id|order_id|e-mail
100       |2567    |jack@abc.com
100       |4784    |jack@abc.com
450       |2578    |lisa@abc.com
450       |8432    |lisa@abc.com
600       |9032    |john@abc.com
600       |9033    |john@abc.com

Table B还有idorder_no列以及许多其他列:

Table B

request_id|order_id|e-mail
100       |2563    |oscar@abc.com
300       |4784    |peter@abc.com
600       |9032    |john@abc.com
650       |2578    |bob@abc.com
850       |8432    |alice@abc.com

如您所见,表A中的给定request_id可以多次出现(参见100和450条记录)

我需要查找表A中的所有记录,这些记录在表B中不在order_id中,但具有相等的request_id列值。

对于上面的例子,我期待这样的事情:

Output

request_id|order_id|e-mail
100       |2567    |jack@abc.com
100       |4784    |jack@abc.com
450       |2578    |lisa@abc.com
450       |8432    |lisa@abc.com
600       |9033    |john@abc.com

正如您所见,表A中的记录不在表B中。只有order_id=600

的记录才满足此标准

我创建了T-SQL查询草图:

select 
    D.request_id, D.order_id 
from 
    table A AS D 
where 
    D.request_id = 600 
    and D.order_id not in (select M.order_id 
                           from table B AS M 
                           where M.request_id = 600)

很遗憾,我不知道如何转换所有request_id的查询。第一个想法是对表A中的所有request_id使用while循环,但在SQL世界中似乎并不聪明。

感谢您的帮助

5 个答案:

答案 0 :(得分:2)

select request_id, order_id from table_a
except
select request_id, order_id from table_b

编辑:这在MS SQL中不起作用:

如果您还想要电子邮件地址:

select request_id, order_id, email from table_a
where (request_id, order_id) not in (
    select request_id, order_id from table_b
)

答案 1 :(得分:2)

试试这个 -

SELECT a.*
FROM      table_a a
LEFT JOIN table_b b ON a.request_id = b.request_id
                   AND a.order_id   = b.order_id
WHERE b.request_id IS NULL

点击此处 - SQL Fiddle

答案 2 :(得分:0)

 SELECT  request_id, order_id,e-mail
  FROM  table_a
  EXCEPT 
  SELECT  request_id, order_id,e-mail
  FROM  table_b

答案 3 :(得分:0)

我不确定你的意思是“但是有相同的request_id列值”。等于什么?

如果您只是希望table_a中的所有记录在table_b中没有记录,并且匹配request_id和order_id,那么:

select a.request_id, a.order_id
from table_a a
where not exists (select * from table_b b where b.request_id=a.request_id 
  and b.order_id=a.order_id)

答案 4 :(得分:-1)

原始海报的问题陈述是(我引用):

  

我需要找到表A中的所有记录,   表B中没有order_id,   但是具有相等的request_id列值。

关于SQL的事情是具有强大的数学基础

  • 设定理论,
  • 谓词演算

因此,O.P的问题陈述可以在标准SQL中简单地重述,几乎就是:

select *                                    -- select all rows
from A a                                    -- from table A 
where exists (                              -- * that have at least one matching
        select *                            --   row in B with the same request ID
        from B b                            --
        where b.request_id = a.request_id   --
      )                                     --
  and not exists (                          -- * but have no matching row in B
        select *                            --   with the same
        from B b                            --   request AND order IDs
        where b.request_id = a.request_id   --
          and b.order_id   = a.order_id     --
      )                                     --

容易!

相关问题