mysql 常用语句

来源:互联网 发布:陈翔6点半之网络大电影 编辑:程序博客网 时间:2024/06/05 18:40

一:对某一个字段分组后查找重复记录:
(关键词:having)
select station_id,count(*) as count from historyenergy_logs where DATE_FORMAT(historytime,’%Y-%m-%d’)=DATE_FORMAT(‘2017-11-22’,’%Y-%m-%d’) and type=0 group by station_id having count>1;

二:查找某几个月的总发电量,如果没有,则定义发电量为0,同时去掉重复的部分,拿到重复数据中最大电量的(eg:得到5-9月的总发电量,同时去掉重复月份的发电量)
(关键词:ifnull,max(xx) group by)
select IFNULL(sum(historyenergy_logs_group_by_month.max_energy),0) as five_to_nine_month_energy from (select month,max(energy) as max_energy from historyenergy_logs where station_id=xxx and month in(5,6,7,8,9) and type=1 group by month order by energy) historyenergy_logs_group_by_month

三:去重标志:
(关键词:distinct )
select sum( distinct energy) from historyenergy_logs where station_id=xxx and (month=5 or month=6 or month=7 or month=8 or month=11) and type=1 group by month order by energy desc

更多关于mysql时间日期数据查询请参考一篇不错的文档;http://www.111cn.net/database/mysql/52975.htm

原创粉丝点击