MySQL 里面的Where 和Having和Count 和distinct和Group By对比

时间:2023-03-09 00:20:16
MySQL 里面的Where 和Having和Count 和distinct和Group By对比
mysql> select accid as uid,date(datetime) AS datetime from game.logLogin GROUP BY accid HAVING datetime='2013-8-20';
+---------+------------+
| uid | datetime |
+---------+------------+
| | -- |
| | -- |
+---------+------------+
rows in set (0.00 sec)

而实际的例子是

mysql> select accid as uid,date(datetime) AS datetime from game.logLogin HAVING datetime='2013-8-20';
+---------+------------+
| uid | datetime |
+---------+------------+
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
+---------+------------+
rows in set (0.00 sec)

用大腿想都会不对

mysql> select distinct accid as uid from (select accid,date(datetime) AS datetime from game.logLogin HAVING datetime='2013-8-20') as t;
+---------+
| uid |
+---------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
+---------+
rows in set (0.00 sec)

当然如何不用 HAVING 和 DISTINCT 和 COUNT 还有GROUP By 的话是可以找出记录的

mysql> select accountID as uid,date(signTime) AS signTime from platform.account HAVING signTime='2013-8-20';
+---------+------------+
| uid | signTime |
+---------+------------+
| | -- |
| | -- |
+---------+------------+
rows in set (0.00 sec) mysql> select accountID as uid,date(signTime) AS signTime from platform.account HAVING signTime='2013-8-19';
+---------+------------+
| uid | signTime |
+---------+------------+
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
| | -- |
+---------+------------+
rows in set (0.00 sec)