刷力扣的時候認(rèn)識的一些好用函數(shù)
#1485.按日期分組銷售產(chǎn)品
https://leetcode-cn.com/problems/group-sold-products-by-the-date/
表 Activities:
+-------------+---------+
| 列名 | 類型 |
+-------------+---------+
| sell_date | date |
| product | varchar |
+-------------+---------+
此表沒有主鍵,它可能包含重復(fù)項淳梦。
此表的每一行都包含產(chǎn)品名稱和在市場上銷售的日期。
編寫一個 SQL 查詢來查找每個日期螃宙、銷售的不同產(chǎn)品的數(shù)量及其名稱谆扎。
每個日期的銷售產(chǎn)品名稱應(yīng)按詞典序排列。
返回按 sell_date 排序的結(jié)果表。
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')
查詢結(jié)果格式如下例所示斥季。
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.逗號分隔(默認(rèn))
2.[]這里的都是可選的,根據(jù)要求來
3.最后別忘了group by
參考 https://blog.csdn.net/u012620150/article/details/81945004
#1495上月播放的兒童適宜電影
原題比較簡單,涉及日期轉(zhuǎn)換的函數(shù)
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. 產(chǎn)品名稱格式修復(fù)
鏈接:https://leetcode-cn.com/problems/fix-product-name-format
表:Sales
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| sale_id | int |
| product_name | varchar |
| sale_date | date |
+--------------+---------+
sale_id 是該表主鍵
該表的每一行包含了產(chǎn)品的名稱及其銷售日期
寫一個 SQL 語句報告:
product_name 是小寫字母且不包含前后空格
sale_date 格式為 ('YYYY-MM')
total 是產(chǎn)品在本月銷售的次數(shù)
返回結(jié)果以 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')
查詢結(jié)果格式如下所示:
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,請注意產(chǎn)品名稱是小寫的渊季,中間可能包含空格
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.執(zhí)行順序炊林,這樣就清楚為啥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
會持續(xù)更新噠~