一:对某一个字段分组后查找重复记录:
(关键词: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
order by 根据指定字段,指定字段中某些内容进行排序:
eg:根据id的后四位进行排序。 id:SH-SD-2018-0012
select * from projects where province=? order by (right(id,4)) desc ;
更多关于mysql时间日期数据查询请参考一篇不错的文档;http://www.111cn.net/database/mysql/52975.htm