sql查询< >甚至填充空行

sc4hvdpw  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(414)

我有两个表,有40000多条记录:表a(列:编号和分类)和表b(列:编号和分类)。我必须从两个不相等的表中提取代码。

SELECT DISTINCT
  a.number,
  a.classification,
  b.classification
FROM TableA a
LEFT OUTER JOIN TableB b
  ON b.number = a.number
  AND a.classification != b.classification;

此查询填充所有不相等的记录,包括空白行。我想要一个没有空行的查询。
所以我在查询中添加了另一行:

SELECT DISTINCT
  a.number,
  a.classification,
  b.classification
FROM TableA a
LEFT OUTER JOIN TableB b
  ON b.number = a.number
  AND a.classification != b.classification
  AND a.classification IS NOT NULL
  AND b.classification IS NOT NULL;

但是上面的查询运行了35分钟,没有生成任何记录。有人能帮我换一个吗?

c9qzyr3d

c9qzyr3d1#

请试试这个->a.classification

select distinct a.number, a.classification, b.classification 
from TableA a left outer join 
TableB b on b.number = a.number
and a.classification != b.classification
AND a.classification <> '' AND a.classification IS NOT NULL
AND b.classification <> '' AND b.classification IS NOT NULL
uplii1fm

uplii1fm2#

你就快到了:

select distinct a.number, a.classification, b.classification 
from      TableA a 
left join TableB b 
       on b.number = a.number
      and a.classification = b.classification
where b.number is null;

首先查找相等的,然后选择找不到的。
或者,因为您显然想要b.classification值:

select distinct a.number, a.classification, b.classification 
from      TableA a 
left join TableB b 
       on b.number = a.number
where a.classification != b.classification;

相关问题