4

我有两个表 ECRDTL_del 和 ECRDTL_edit,我只想显示 ECRDTL_del 中不在 ECRDTL_edit 中的记录。我曾尝试使用 MYSQL 左连接,但无法获得所需的结果。这是我当前的查询字符串:

SELECT a.*
FROM ECRDTL_del AS a
    LEFT JOIN ECRDTL_edit AS b ON b.Ecrno = a.Ecrno
WHERE b.Cylno <> a.Cylno

下面提到的这两个表和预期结果的数据示例:

ECRDTL_del:

Ecrno  Cylno
9090   8881
9090   8882
9090   8883
9090   8884

ECRDTL_编辑:

Ecrno  Cylno
9090   8881
9090   8885
9090   8886
9090   8884

LEFT 加入后的预期结果:

Ecrno  Cylno
9090   8882
9090   8883
4

3 回答 3

12

从你的问题:

“...我只想显示 ECRDTL_del 中不在 ECRDTL_edit 中的记录。”

你很接近,你只需要检查是否b.Ecrno IS NULL

SELECT a.* 
FROM   ECRDTL_del AS a 
       LEFT JOIN ECRDTL_edit AS b 
          ON a.Ecrno = b.Ecrno AND 
             a.Cylno = b.Cylno
WHERE  b.Ecrno IS NULL

输出

╔═══════╦═══════╗
║ ECRNO ║ CYLNO ║
╠═══════╬═══════╣
║  9090 ║  8882 ║
║  9090 ║  8883 ║
╚═══════╩═══════╝
于 2013-04-10T12:53:02.153 回答
3

我通常EXISTS用于这种类型的事情:

SELECT *
FROM ECRDTL_del as a
WHERE NOT EXISTS(
    SELECT *
    FROM ECRDTL_edit as b
    WHERE b.Ecrno = a.Ecrno
    AND b.Cylno = a.Cylno
)

一旦找到匹配的记录,它就会停止,而类似 an 的东西IN会继续搜索匹配的记录。

于 2013-04-10T13:01:57.477 回答
1
select * from ECRDTL_del where Cylno not in (select Cylno from ECRDTL_edit)
于 2013-04-10T12:52:26.387 回答