如何从一个表中选择所有在mysql中两个其他表的联合中不存在的记录

时间:2019-03-07 07:19:37

标签: mysql union

我想返回两个其他表的并集中不存在的所有记录,但是当我尝试封装并集的结果时,工作台将返回语法错误。我在做什么错了?

SELECT TransactionID
from transactions 
where transactionid not in

(
(select distinct Transactionid
from transactionsdebits 
where   accountid like '81%')

union

(select distinct TransactionID
from transactionscredits
where accountid like '81%')
)
;

1 个答案:

答案 0 :(得分:1)

您可以在下面尝试-

    SELECT TransactionID
    from transactions 
    where transactionid not in ( select transactionid from
     (
       select distinct Transactionid
       from transactionsdebits 
       where   accountid like '81%'
       union
       select distinct TransactionID
       from transactionscredits
       where accountid like '81%'
     )
   A)