无法将JOIN中的非匹配行计为NULL

时间:2018-05-23 00:45:40

标签: sql postgresql

我有这个查询加入两个“假”表:

select count(tb2.col2) from (select unnest(array['A','B']) col1) tb1 left join (select unnest(array['B','C']) col2) tb2 on tb1.col1=tb2.col2 where tb2.col2 IS NULL;

http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/22712

您可以看到我希望NULL的计数为1,但它显示0

那么如何统计不匹配的行?

 col1 | col2
------+------
 A    |
 B    | B

1 个答案:

答案 0 :(得分:2)

使用select count(*)代替select count(tb2.col2)

  • count(*)是行数。
  • count(expr)expr非空的行数。