【Mysql】日期、行变列(IF、CASE WHEN THEN)语句总结
2023-09-27 14:26:28 时间
- 获取昨天的日期
日期相减函数 datediff
select datediff(curdate(),INTERVAL 1 DAY) ;
- 删除Person表中重复的Email数据,只保留id最小的一条
delete p1 from Person p1,Person p2 where p1.Email = p2.Email and p1.Id > p2.Id;
- 换座位,1与2换,3与4换,5保留原来的
COALESCE空值表达式,第一个不为空,返回第一个结束;否则继续判断接下来的数据;
select id, COALESCE(s2.student,s1.student) from seat s1, LEFT JOIN
seat s2 ON ((s1.id + 1) ^ 1) - 1 = s2.id
ORDER BY s1.id;
- 交换性别,‘f’ 变 ‘m’, ‘m’ 变 ‘f’
update salary set sex=
case sex when 'f' then 'm'
else 'f'
END;
- 月份列变行,求总和,并汇总
if(month=‘Jan’,revenue,null) Jan_Revenue
case month when ‘Jan’ then revenue END Jan_Revenue
select id,
sum(if(month='Jan',revenue,null)) Jan_Revenue,
sum(if(month='Feb',revenue,null)) Feb_Revenue,
sum(if(month='Mar',revenue,null)) Mar_Revenue,
sum(if(month='Apr',revenue,null)) Apr_Revenue,
sum(if(month='May',revenue,null)) May_Revenue,
sum(if(month='Jun',revenue,null)) Jun_Revenue,
sum(if(month='Jul',revenue,null)) Jul_Revenue,
sum(if(month='Aug',revenue,null)) Aug_Revenue,
sum(if(month='Sep',revenue,null)) Sep_Revenue,
sum(if(month='Oct',revenue,null)) Oct_Revenue,
sum(if(month='Nov',revenue,null)) Nov_Revenue,
sum(if(month='Dec',revenue,null)) Dec_Revenue from Department group by id;
select id,
sum(case month when 'Jan' then revenue END) Jan_Revenue,
sum(case month when 'Feb' then revenue END) Feb_Revenue,
sum(case month when 'Mar' then revenue END) Mar_Revenue,
sum(if(month='Apr',revenue,null)) Apr_Revenue,
sum(if(month='May',revenue,null)) May_Revenue,
sum(if(month='Jun',revenue,null)) Jun_Revenue,
sum(if(month='Jul',revenue,null)) Jul_Revenue,
sum(if(month='Aug',revenue,null)) Aug_Revenue,
sum(if(month='Sep',revenue,null)) Sep_Revenue,
sum(if(month='Oct',revenue,null)) Oct_Revenue,
sum(if(month='Nov',revenue,null)) Nov_Revenue,
sum(if(month='Dec',revenue,null)) Dec_Revenue from Department group by id;
相关文章
- mysql的学习记录
- MySQL通过sql语句获取当前日期|时间|时间戳
- MySQL分布值很少的字段要不要加索引
- 在idea中SpringBoot2.4.1项目下,会默认使用mysql-connector-java-8.0.22驱动的错误原因
- 在 MySQL 中处理日期和时间 - 第二章节
- 在 MySQL 中处理日期和时间 - 第五章节
- [转]MySQL DATE_FORMAT() 函数
- MySQL: 日期函数
- MySQL: configuration
- Mysql date_to_str()函数 日期格式化详细解析
- mysql获取日期(将时间戳转换成短日期格式)
- 转 MySQL与Oracle 差异比较之一数据类型
- MySQL主从复制状态及数据一致性监测工具
- Mysql中处理1970年前的日期(unixtime为负数的情况)负数时间戳格式化
- Mysql日期函数说明
- MySql 数据库中查询日期函数的使用
- Mysql MGR(1)MGR基础概念
- (1.14)mysql锁问题之MyIsam
- MySQL C#连接MySQL保存当前时间,时分秒都是0,只有日期