sql查询< >甚至填充空行

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

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

  1. SELECT DISTINCT
  2. a.number,
  3. a.classification,
  4. b.classification
  5. FROM TableA a
  6. LEFT OUTER JOIN TableB b
  7. ON b.number = a.number
  8. AND a.classification != b.classification;

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

  1. SELECT DISTINCT
  2. a.number,
  3. a.classification,
  4. b.classification
  5. FROM TableA a
  6. LEFT OUTER JOIN TableB b
  7. ON b.number = a.number
  8. AND a.classification != b.classification
  9. AND a.classification IS NOT NULL
  10. AND b.classification IS NOT NULL;

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

c9qzyr3d

c9qzyr3d1#

请试试这个->a.classification

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

uplii1fm2#

你就快到了:

  1. select distinct a.number, a.classification, b.classification
  2. from TableA a
  3. left join TableB b
  4. on b.number = a.number
  5. and a.classification = b.classification
  6. where b.number is null;

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

  1. select distinct a.number, a.classification, b.classification
  2. from TableA a
  3. left join TableB b
  4. on b.number = a.number
  5. where a.classification != b.classification;

相关问题