关于COUNT( DISTINCT。)问题

MySql 码拜 8年前 (2016-02-10) 935次浏览
   这是官方手册的一句话:
In MySQL, you can obtain the number of distinct expression combinations that do not contain NULL by giving a list of expressions. In standard SQL, you would have to do a concatenation of all expressions inside COUNT(DISTINCT …).
能解释一下吗?
解决方案

20

就是说在mysql中,可以这么写,里面可以有多个字段count(distinct 多个字段):

mysql> select count(distinct id,v) from tb_m;
+--+
| count(distinct id,v) |
+--+
|                    1 |
+--+
1 row in set (0.04 sec)

这个是mysql特有的语法,而一般其他的数据库,例如oracle,sql server,只能这么写一个字段count(distinct 一个字段):

mysql> select count(distinct id) from tb_m;
+--+
| count(distinct id) |
+--+
|                  1 |
+--+
1 row in set (0.12 sec)

20

select count(*) from (select distinct id from tb_m) t

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于COUNT( DISTINCT。)问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)