演示
create table mytable (c1 string,c2 string);
insert into mytable values ('A','A'),('B',null),('C',null);
select count(distinct c1) as count_distinct from mytable;
+----------------+
| count_distinct |
+----------------+
| 3 |
+----------------+
正如预期的那样 - 3 个不同的值 - 'A'、'B' 和 'C'
select count(distinct concat(c1,c2)) as count_distinct from mytable;
+----------------+
| count_distinct |
+----------------+
| 1 |
+----------------+
正如预期的那样。为什么?- 见下一个查询
select c1,c2,concat(c1,c2) as concat_c1_c2 from mytable;
+----+------+--------------+
| c1 | c2 | concat_c1_c2 |
+----+------+--------------+
| A | A | AA |
| B | NULL | NULL |
| C | NULL | NULL |
+----+------+--------------+
与 NULL 连接产生 NULL
select count(distinct c1,c2) as count_distinct from mytable;
+----------------+
| count_distinct |
+----------------+
| 1 |
+----------------+
漏洞!!
这是解决该错误的方法:
select count(distinct struct(c1,c2)) as count_distinct from mytable;
+----------------+
| count_distinct |
+----------------+
| 3 |
+----------------+