【Mysql】日期、行变列(IF、CASE WHEN THEN)语句总结
生活随笔
收集整理的這篇文章主要介紹了
【Mysql】日期、行变列(IF、CASE WHEN THEN)语句总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 獲取昨天的日期
日期相減函數 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 JOINseat 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】日期、行变列(IF、CASE WHEN THEN)语句总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PostgreSQL:创建自增序列id,
- 下一篇: Java程序启动同时复制resource