MySQL 里面的Where 和Having和Count 和distinct和Group By对比
2014-12-03 11:46
344 查看
mysql> select accid as uid,date(datetime) AS datetime from game.logLogin GROUP BY accid HAVING datetime='2013-8-20'; +---------+------------+ | uid | datetime | +---------+------------+ | 1000010 | 2013-08-20 | | 1000012 | 2013-08-20 | +---------+------------+ 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 | +---------+------------+ | 1000004 | 2013-08-20 | | 1000004 | 2013-08-20 | | 1000001 | 2013-08-20 | | 1000000 | 2013-08-20 | | 1000004 | 2013-08-20 | | 1000004 | 2013-08-20 | | 1000012 | 2013-08-20 | | 1000010 | 2013-08-20 | | 1000000 | 2013-08-20 | | 1000002 | 2013-08-20 | | 1000006 | 2013-08-20 | | 1000003 | 2013-08-20 | | 1000003 | 2013-08-20 | | 1000012 | 2013-08-20 | | 1000003 | 2013-08-20 | | 0 | 2013-08-20 | | 1000012 | 2013-08-20 | +---------+------------+ 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 | +---------+ | 1000004 | | 1000001 | | 1000000 | | 1000012 | | 1000010 | | 1000002 | | 1000006 | | 1000003 | | 0 | +---------+ 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 | +---------+------------+ | 1000013 | 2013-08-20 | | 1000014 | 2013-08-20 | +---------+------------+ 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 | +---------+------------+ | 1000000 | 2013-08-19 | | 1000001 | 2013-08-19 | | 1000002 | 2013-08-19 | | 1000003 | 2013-08-19 | | 1000004 | 2013-08-19 | | 1000005 | 2013-08-19 | | 1000006 | 2013-08-19 | | 1000007 | 2013-08-19 | | 1000008 | 2013-08-19 | | 1000009 | 2013-08-19 | | 1000010 | 2013-08-19 | | 1000011 | 2013-08-19 | | 1000012 | 2013-08-19 | +---------+------------+ rows in set (0.00 sec)
相关文章推荐
- MySQL 里面的Where 和Having和Count 和distinct和Group By对比
- 6.组函数(avg(),sum(),max(),min(),count())、多行函数,分组数据(group by,求各部门的平均工资),分组过滤(having和where),sql优化
- 6.组函数(avg(),sum(),max(),min(),count())、多行函数,分组数据(group by,求各部门的平均工资),分组过滤(having和where),sql优化
- 【mysql】【查询的五种语法where、group by、having、order by、limit】
- 组函数(avg(),sum(),max(),min(),count())、多行函数,分组数据(group by,求各部门的平均工资),分组过滤(having和where),sql优化 .
- mysql:distinct与group by 效率对比
- Mysql中Group By使用Having语句配合查询(where和having区别)
- mysql关键字讲解(join 、order by、group by、having、distinct)
- 聚合函数(sum、count、max、min、avg)、where、group by、having的组合用法实例
- [mysql] select的子句 where,group by, having, order by, limit的使用顺序及实例
- Mysql数据分组GROUP BY 和HAVING,与WHERE组合使用
- mysql中的group by,having,order by,where用法
- Mysql数据分组GROUP BY 和HAVING,与WHERE组合使用
- mysql 知识点sum、count、group by、having、ifnull()、left join
- mysql 的DISTINCT、EXISTS、IN、GROUP BY..HAVING 用法记录
- mysql关键字讲解(join 、order by、group by、having、distinct)
- mysql 中 where 、 group by 、having、order by 的执行顺序
- 关于mysql中 group by , order by , where, having 语句的区别与运用
- mysql的group by、order by和having、where比较
- mysql---select的五种子句学习(where、group by、having、order by、limit)