刷力扣的时候认识的一些好用函数
#1485.按日期分组销售产品
https://leetcode-cn.com/problems/group-sold-products-by-the-date/
表 Activities:
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| sell_date | date |
| product | varchar |
+-------------+---------+
此表没有主键,它可能包含重复项。
此表的每一行都包含产品名称和在市场上销售的日期。
编写一个 SQL 查询来查找每个日期、销售的不同产品的数量及其名称。
每个日期的销售产品名称应按词典序排列。
返回按 sell_date 排序的结果表。
Create table If Not Exists Activities (sell_date date, product varchar(20))
Truncate table Activities
insert into Activities (sell_date, product) values ('2020-05-30', 'Headphone')
insert into Activities (sell_date, product) values ('2020-06-01', 'Pencil')
insert into Activities (sell_date, product) values ('2020-06-02', 'Mask')
insert into Activities (sell_date, product) values ('2020-05-30', 'Basketball')
insert into Activities (sell_date, product) values ('2020-06-01', 'Bible')
insert into Activities (sell_date, product) values ('2020-06-02', 'Mask')
insert into Activities (sell_date, product) values ('2020-05-30', 'T-Shirt')
查询结果格式如下例所示。
Activities 表:
+------------+-------------+
| sell_date | product |
+------------+-------------+
| 2020-05-30 | Headphone |
| 2020-06-01 | Pencil |
| 2020-06-02 | Mask |
| 2020-05-30 | Basketball |
| 2020-06-01 | Bible |
| 2020-06-02 | Mask |
| 2020-05-30 | T-Shirt |
+------------+-------------+
Result 表:
+------------+----------+------------------------------+
| sell_date | num_sold | products |
+------------+----------+------------------------------+
| 2020-05-30 | 3 | Basketball,Headphone,T-shirt |
| 2020-06-01 | 2 | Bible,Pencil |
| 2020-06-02 | 1 | Mask |
+------------+----------+------------------------------+
对于2020-05-30,出售的物品是 (Headphone, Basketball, T-shirt),按词典序排列,并用逗号 ',' 分隔。
对于2020-06-01,出售的物品是 (Pencil, Bible),按词典序排列,并用逗号分隔。
对于2020-06-02,出售的物品是 (Mask),只需返回该物品名。
方法
select sell_date, count(*) as num_sold,
group_concat(product order by product) as products
from(select distinct sell_date,product from Activities) a
group by sell_date
order by sell_date
group_concat( [DISTINCT] 要连接的字段 [Order BY 排序字段 ASC/DESC] [Separator ‘分隔符’] )
1.逗号分隔(默认)
2.[]这里的都是可选的,根据要求来
3.最后别忘了group by
参考 https://blog.csdn.net/u012620150/article/details/81945004
#1495上月播放的儿童适宜电影
原题比较简单,涉及日期转换的函数
date_format(program_date,"%Y-%m")="2020-06"
注意Y大写,小写的话是20-06
“2020年”的11种写法
YEAR(sale_date) = 2020
sale_date LIKE '2020%'
sale_date REGEXP '^2020'
LEFT(sale_date,4) = '2020'
MID(sale_date,1,4) = '2020'
SUBSTR(sale_date,1,4) = '2020'
DATE_FORMAT(sale_date,'%Y') = 2020
EXTRACT(YEAR FROM sale_date) = 2020
sale_date BETWEEN '2020-01-01' AND '2020-12-31'
sale_date > '2019-12-31' AND sale_date < '2021-01-01'
sale_date >= '2020-01-01' AND sale_date <= '2020-12-31'
#1543. 产品名称格式修复
链接:https://leetcode-cn.com/problems/fix-product-name-format
表:Sales
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| sale_id | int |
| product_name | varchar |
| sale_date | date |
+--------------+---------+
sale_id 是该表主键
该表的每一行包含了产品的名称及其销售日期
写一个 SQL 语句报告:
product_name 是小写字母且不包含前后空格
sale_date 格式为 ('YYYY-MM')
total 是产品在本月销售的次数
返回结果以 product_name 升序 排列,如果有排名相同, 再以 sale_date 升序 排列。
Create table If Not Exists Sales (sale_id int, product_name varchar(30), sale_date date)
Truncate table Sales
insert into Sales (sale_id, product_name, sale_date) values ('1', 'LCPHONE', '2000-01-16')
insert into Sales (sale_id, product_name, sale_date) values ('2', 'LCPhone', '2000-01-17')
insert into Sales (sale_id, product_name, sale_date) values ('3', 'LcPhOnE', '2000-02-18')
insert into Sales (sale_id, product_name, sale_date) values ('4', 'LCKeyCHAiN', '2000-02-19')
insert into Sales (sale_id, product_name, sale_date) values ('5', 'LCKeyChain', '2000-02-28')
insert into Sales (sale_id, product_name, sale_date) values ('6', 'Matryoshka', '2000-03-31')
查询结果格式如下所示:
Sales 表:
+------------+------------------+--------------+
| sale_id | product_name | sale_date |
+------------+------------------+--------------+
| 1 | LCPHONE | 2000-01-16 |
| 2 | LCPhone | 2000-01-17 |
| 3 | LcPhOnE | 2000-02-18 |
| 4 | LCKeyCHAiN | 2000-02-19 |
| 5 | LCKeyChain | 2000-02-28 |
| 6 | Matryoshka | 2000-03-31 |
+------------+------------------+--------------+
Result 表:
+--------------+--------------+----------+
| product_name | sale_date | total |
+--------------+--------------+----------+
| lcphone | 2000-01 | 2 |
| lckeychain | 2000-02 | 2 |
| lcphone | 2000-02 | 1 |
| matryoshka | 2000-03 | 1 |
+--------------+--------------+----------+
1 月份,卖了 2 个 LcPhones,请注意产品名称是小写的,中间可能包含空格
2 月份,卖了 2 个 LCKeychains 和 1 个 LCPhone
3 月份,卖了 1 个 matryoshka
方法
select lower(trim(product_name)) as product_name,date_format(sale_date,"%Y-%m") as sale_date,count(*) as total
from Sales
group by lower(replace(product_name," ","")),date_format(sale_date,"%Y-%m")
order by product_name asc,sale_date asc
1.trim() 去除字符串首尾空格 可以用 replace(product_name," ","") 替代。
2.lower() 全部变成小写 upper()全部变成大写
3.执行顺序,这样就清楚为啥group by 后边不可以跟别名(mysql 除外)
https://blog.csdn.net/u013887008/article/details/93377939
⑦ select
⑧ distinct
① from table 1
③ join table 2
② on 连接条件
④ where
⑤ group by
⑥ having
⑨ order by
⑩ limit
会持续更新哒~