设计公司网站怎么做,PS做图标兼职网站,电商软件什么品牌好,创业网站模板前述
知识点学习#xff1a;
SQL 日期函数 day() 、month()、year() 各种使用方法mysql 两个字符年月拼接
题目描述
leetcode题目#xff1a;1193. 每月交易 I 思路
先按照年月排#xff0c;再按照country排列
日期拼接相关的函数
year(): 截取年份#xff1b;month…前述
知识点学习
SQL 日期函数 day() 、month()、year() 各种使用方法mysql 两个字符年月拼接
题目描述
leetcode题目1193. 每月交易 I 思路
先按照年月排再按照country排列
日期拼接相关的函数
year(): 截取年份month()截取月份day(): 截取日期concat()字符串拼接LPAD() 在月份前补齐0确保月份是两位数。- 分隔符举例‘2018-12’ 中的 ‘-’DATE_FORMAT()格式化日期。 DATE_FORMAT(2018-12-23, %Y-%m)
注意对比区分
count(if(stateapproved, 1, null))sum(if(stateapproved, 1, 0))
写法一
selectconcat(year(trans_date), -, lpad(month(trans_date), 2, 0)) as month,country,count(id) as trans_count,sum(if(stateapproved, 1, 0)) as approved_count,sum(amount) as trans_total_amount,sum(if(stateapproved, amount, 0)) as approved_total_amount
from Transactions
group by year(trans_date), month(trans_date) , country执行用时分布601ms击败57.76%使用 MySQL 的用户
写法二
select date_format(trans_date, %Y-%m) as month,country,count(id) as trans_count,count(if(stateapproved, 1, null)) as approved_count,sum(amount) as trans_total_amount,sum(if(stateapproved, amount, 0)) as approved_total_amount
from Transactions
group by month, country执行用时分布 852ms击败28.66%使用 MySQL 的用户