2013.10.13 很好的SQL语句
MYSQL-
1.将查询结果中的0/1分别显示女/男
SELECT CASE sexFROM carsystem.user
2.删除表的一列
alter table TEST_USER drop column age
3.只更新当天登陆的民警的退出时间
Update policemenlogin set quitdate=now() WHERE policemenId='p4' and to_days(logindate)8to_days(now())
4.查过去24小时之内的数据(一下内容参见另一篇文章:2013.10.13 事务特性、MYSQL 触发器、存储过程、函数)
核心:checkDate >DATE_ADD(now(), INTERVAL -1 DAY)
String sql = "select * from records where vehicleId='" + vehicleId+ " order by checkDate DESC";
DATE_ADD(now(),INTERVAL -1 DAY);系统函数,指当前时间减去1天,即过去24小时时间段内
5.按天数统计数据数量
SELECT COUNT(*),records.checkdate FROM records GROUP BY TO_DAYS(records.checkdate);
6.按月份统计数据数量
SELECT COUNT(*),records.checkdate FROM records GROUP BY MONTH(records.checkdate);
7.获取当天数据
select * from policemenlogin where DATE_FORMAT(logindate,'%Y-%m-%d')=CURDATE();//取当天数据
8.按月份分组
SELECT DATE_FORMAT(time,'%Y%m') t,COUNT(*) '数量' FROM `user` GROUP BY PERIOD_DIFF( t,'200001');
PERIOD_DIFF( t,'200001');//返回2000年01月到t时间的总月份数