1. 一行轉多行 substring_index
1)將一行拆分為多行
select a.id,
substring_index(substring_index(a.ids,
",", b.help_topic_id +1)
,",",-
1)as ids
from inini a
join mysql.help_topic b
on a.id =
2and b.help_topic_id <
(length(a.ids)
- length(
replace
(a.ids,
",","")
)+1)
原始資料:
轉換結果:
2)將某結果集欄位轉為集合作為條件使用
結果:
2. 多行轉一行 concat
3. case 在 select、from、where 後的使用示例
4. 日期處理
日期格式:
計算使用者名稱下的總收益、月收益、日收益:
@query
(value =
"select s.user_id as userid, "
+"sum(o.amount) as count, "
+"sum(if(o.time like concat(substr(curdate(),1,8),'%'), o.amount, 0)) as monthcount, "
+"sum(if(o.time like concat(curdate(),'%'), o.amount, 0)) as daycount, "
+"sum(if(isnull(o.extract_id),o.amount,0)) as surplus "
+"from store s "
+"left join order o "
+"on o.store_id =s.id "
+"where o.state = 0 and s.user_id = ?1 "
+"group by s.user_id"
, nativequery =
true
) map getonlinecensus
(long userid)
;
計算一周內的每日收益:
@query
(value =
"select substr(o.time,1,10)as time, "
+"sum(if(o.state = 0, o.amount, 0)) as count "
+"from store s "
+"left join order o "
+"on o.store_id = s.id "
+"where o.state=0 "
+"and o.time > date_sub(curdate(), interval 7 day) "
+"and s.user_id = ?1 "
+"group by time, s.user_id "
+"order by time "
, nativequery =
true
) list
getonlineline
(long userid)
;
這個是spring data jpa專案裡的sql
還沒寫完,不管 先發了
mysql 記錄函式 mysql 函式記錄
條件查詢 case case when then when then when then else end1.簡單case函式寫法 注意 的位置 select case when 1 then 男 when 0 then 女 else 保密 end as text from user 2.case搜...
Mysql函式記錄
1.instr srt,str 函式,得到字元第一次出現所在的位置,位置從1開始 select instr 1234 3 3 2.substring str,num 擷取字串,從索引位置擷取,包括索引位置,位置從1開始 select substring 123 2 23 3.concat str,連...
Mysql函式記錄
1.當前時間,精確到時分秒 now 2.當前日期,精確到日 年月日 curdate 3.當前年份 year now 4.當前月 month 日期字段 5.年月日 from unixtime 日期字段,y 年 from unixtime 日期字段,m 月 from unixtime 日期字段,y m ...