mysql 常用语句

一:对某一个字段分组后查找重复记录:
(关键词: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值