MySQL 多表查询、连接查询(内连接、外连接)
文章目錄
- 1. 多表查詢
- 2. 連接查詢
- 練習(xí) LeetCode 175. 組合兩個(gè)表
- 練習(xí) LeetCode 181. 超過經(jīng)理收入的員工
- 練習(xí) LeetCode 1378. 使用唯一標(biāo)識碼替換員工ID
- 練習(xí) LeetCode 1068. 產(chǎn)品銷售分析 I
- 練習(xí) LeetCode 1069. 產(chǎn)品銷售分析 II
- 練習(xí) LeetCode 1303. 求團(tuán)隊(duì)人數(shù)
- 練習(xí) LeetCode 1350. 院系無效的學(xué)生
- 練習(xí) LeetCode 613. 直線上的最近距離
- 練習(xí) LeetCode 1251. 平均售價(jià)
- 練習(xí) LeetCode 577. 員工獎(jiǎng)金
- 練習(xí) LeetCode 1327. 列出指定時(shí)間段內(nèi)所有的下單產(chǎn)品
- 練習(xí) LeetCode 603. 連續(xù)空余座位
- 練習(xí) LeetCode 607. 銷售員
- 練習(xí) LeetCode 1294. 不同國家的天氣類型
學(xué)習(xí)自 廖雪峰的官方網(wǎng)站
1. 多表查詢
SELECT * FROM <表1> <表2> SELECT * FROM students, classes;查詢的結(jié)果是一個(gè)二維表,它是students表和classes表的“乘積”,即students表的每一行與classes表的每一行都兩兩拼在一起返回
結(jié)果集的列數(shù)是兩表的列數(shù)之和,行數(shù)是兩表行數(shù)之積(要小心,乘積有可能很大)。
- 對一樣的列名,起別名區(qū)分(下面的name、id)
- 表也可以起別名,方便簡寫
- 多表查詢也可以使用WHERE條件
2. 連接查詢
連接查詢是另一種類型的多表查詢。
連接查詢對多個(gè)表進(jìn)行JOIN運(yùn)算:
- 先確定一個(gè)主表作為結(jié)果集
- 然后,把其他表的行有選擇性“連接”在主表結(jié)果集上
- 最常用的一種內(nèi)連接——INNER JOIN來實(shí)現(xiàn)
-
外連接 LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN’
-
區(qū)別:哪邊的表的數(shù)據(jù)完全保留,另一個(gè)表的數(shù)據(jù)不存在的填NULL
練習(xí) LeetCode 175. 組合兩個(gè)表
題目:
Create table Person (PersonId int, FirstName varchar(255), LastName varchar(255)) Create table Address (AddressId int, PersonId int, City varchar(255), State varchar(255)) Truncate table Person insert into Person (PersonId, LastName, FirstName) values ('1', 'Wang', 'Allen') Truncate table Address insert into Address (AddressId, PersonId, City, State) values ('1', '2', 'New York City', 'New York') 表1: Person +-------------+---------+ | 列名 | 類型 | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId 是上表主鍵表2: Address +-------------+---------+ | 列名 | 類型 | +-------------+---------+ | AddressId | int | | PersonId | int | | City | varchar | | State | varchar | +-------------+---------+ AddressId 是上表主鍵編寫一個(gè) SQL 查詢,滿足條件:無論 person 是否有地址信息, 都需要基于上述兩表提供 person 的以下信息:FirstName, LastName, City, State來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/combine-two-tables
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below SELECTP.FirstName, P.LastName, A.City, A.State FROM Person P LEFT OUTER JOIN Address A ON P.PersonId = A.PersonId369 ms
練習(xí) LeetCode 181. 超過經(jīng)理收入的員工
題目:
Employee 表包含所有員工,他們的經(jīng)理也屬于員工。每個(gè)員工都有一個(gè) Id,此外還有一列對應(yīng)員工的經(jīng)理的 Id。
+----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+給定 Employee 表,編寫一個(gè) SQL 查詢,該查詢可以獲取收入超過他們經(jīng)理的員工的姓名。在上面的表格中,Joe 是唯一一個(gè)收入超過他的經(jīng)理的員工。
+----------+ | Employee | +----------+ | Joe | +----------+來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
把同一份表再次JOIN該表,條件是A.ManagerId = B.Id
或者
# Write your MySQL query statement below SELECT A.Name Employee FROM Employee A INNER JOIN Employee B ON A.ManagerId = B.Id WHERE A.Salary > B.Salary309 ms
練習(xí) LeetCode 1378. 使用唯一標(biāo)識碼替換員工ID
Employees 表:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+id 是這張表的主鍵。
這張表的每一行分別代表了某公司其中一位員工的名字和 ID 。
EmployeeUNI 表:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | unique_id | int | +---------------+---------+(id, unique_id) 是這張表的主鍵。
這張表的每一行包含了該公司某位員工的 ID 和他的唯一標(biāo)識碼(unique ID)。
寫一段SQL查詢來展示每位用戶的 唯一標(biāo)識碼(unique ID );如果某位員工沒有唯一標(biāo)識碼,使用 null 填充即可。
你可以以 任意 順序返回結(jié)果表。
查詢結(jié)果的格式如下例所示:
Employees table: +----+----------+ | id | name | +----+----------+ | 1 | Alice | | 7 | Bob | | 11 | Meir | | 90 | Winston | | 3 | Jonathan | +----+----------+EmployeeUNI table: +----+-----------+ | id | unique_id | +----+-----------+ | 3 | 1 | | 11 | 2 | | 90 | 3 | +----+-----------+EmployeeUNI table: +-----------+----------+ | unique_id | name | +-----------+----------+ | null | Alice | | null | Bob | | 2 | Meir | | 3 | Winston | | 1 | Jonathan | +-----------+----------+Alice and Bob 沒有唯一標(biāo)識碼, 因此我們使用 null 替代。 Meir 的唯一標(biāo)識碼是 2 。 Winston 的唯一標(biāo)識碼是 3 。 Jonathan 唯一標(biāo)識碼是 1 。來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/replace-employee-id-with-the-unique-identifier
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select a.unique_id, b.name from Employees b left join EmployeeUNI a on a.id = b.id練習(xí) LeetCode 1068. 產(chǎn)品銷售分析 I
銷售表 Sales:
+-------------+-------+ | Column Name | Type | +-------------+-------+ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | +-------------+-------+(sale_id, year) 是銷售表 Sales 的主鍵.
product_id 是產(chǎn)品表 Product 的外鍵.
注意: price 表示每單位價(jià)格
產(chǎn)品表 Product:
+--------------+---------+ | Column Name | Type | +--------------+---------+ | product_id | int | | product_name | varchar | +--------------+---------+product_id 是表的主鍵.
寫一條SQL 查詢語句獲取產(chǎn)品表 Product 中所有的 產(chǎn)品名稱 product name 以及 該產(chǎn)品在 Sales 表中相對應(yīng)的 上市年份 year 和 價(jià)格 price。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/product-sales-analysis-i
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select b.product_name, a.year, a.price from Sales a join Product b on a.product_id = b.product_id練習(xí) LeetCode 1069. 產(chǎn)品銷售分析 II
銷售表:Sales
+-------------+-------+ | Column Name | Type | +-------------+-------+ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | +-------------+-------+sale_id 是這個(gè)表的主鍵。
product_id 是 Product 表的外鍵。
請注意價(jià)格是每單位的。
產(chǎn)品表:Product
product_id 是這個(gè)表的主鍵。
編寫一個(gè) SQL 查詢,按產(chǎn)品 id product_id 來統(tǒng)計(jì)每個(gè)產(chǎn)品的銷售總量。
查詢結(jié)果格式如下面例子所示:
Sales 表: +---------+------------+------+----------+-------+ | sale_id | product_id | year | quantity | price | +---------+------------+------+----------+-------+ | 1 | 100 | 2008 | 10 | 5000 | | 2 | 100 | 2009 | 12 | 5000 | | 7 | 200 | 2011 | 15 | 9000 | +---------+------------+------+----------+-------+Product 表: +------------+--------------+ | product_id | product_name | +------------+--------------+ | 100 | Nokia | | 200 | Apple | | 300 | Samsung | +------------+--------------+Result 表: +--------------+----------------+ | product_id | total_quantity | +--------------+----------------+ | 100 | 22 | | 200 | 15 | +--------------+----------------+來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/product-sales-analysis-ii
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select s.product_id, sum(s.quantity) total_quantity fromSales s left join Product pusing(product_id)group by s.product_id # order by s.product_id # 可選排序練習(xí) LeetCode 1303. 求團(tuán)隊(duì)人數(shù)
員工表:Employee
+---------------+---------+ | Column Name | Type | +---------------+---------+ | employee_id | int | | team_id | int | +---------------+---------+employee_id 字段是這張表的主鍵,表中的每一行都包含每個(gè)員工的 ID 和他們所屬的團(tuán)隊(duì)。
編寫一個(gè) SQL 查詢,以求得每個(gè)員工所在團(tuán)隊(duì)的總?cè)藬?shù)。
查詢結(jié)果中的順序無特定要求。
查詢結(jié)果格式示例如下:
Employee Table: +-------------+------------+ | employee_id | team_id | +-------------+------------+ | 1 | 8 | | 2 | 8 | | 3 | 8 | | 4 | 7 | | 5 | 9 | | 6 | 9 | +-------------+------------+ Result table: +-------------+------------+ | employee_id | team_size | +-------------+------------+ | 1 | 3 | | 2 | 3 | | 3 | 3 | | 4 | 1 | | 5 | 2 | | 6 | 2 | +-------------+------------+ ID 為 1、2、3 的員工是 team_id 為 8 的團(tuán)隊(duì)的成員, ID 為 4 的員工是 team_id 為 7 的團(tuán)隊(duì)的成員, ID 為 5、6 的員工是 team_id 為 9 的團(tuán)隊(duì)的成員。來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/find-the-team-size
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
- 自連接,按著 team_id,有重復(fù)的 team_id,產(chǎn)生了笛卡爾積
or
# Write your MySQL query statement below select a.employee_id, temp.team_size from Employee a left join (select team_id, count(*) team_size from Employeegroup by team_id ) temp # on a.team_id = temp.team_id 也可以用 using using(team_id)練習(xí) LeetCode 1350. 院系無效的學(xué)生
院系表: Departments
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+id 是該表的主鍵
該表包含一所大學(xué)每個(gè)院系的 id 信息
學(xué)生表: Students
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | | department_id | int | +---------------+---------+id 是該表的主鍵
該表包含一所大學(xué)每個(gè)學(xué)生的 id 和他/她就讀的院系信息
寫一條 SQL 語句以查詢那些所在院系不存在的學(xué)生的 id 和姓名
可以以任何順序返回結(jié)果
下面是返回結(jié)果格式的例子
Departments 表: +------+--------------------------+ | id | name | +------+--------------------------+ | 1 | Electrical Engineering | | 7 | Computer Engineering | | 13 | Bussiness Administration | +------+--------------------------+Students 表: +------+----------+---------------+ | id | name | department_id | +------+----------+---------------+ | 23 | Alice | 1 | | 1 | Bob | 7 | | 5 | Jennifer | 13 | | 2 | John | 14 | | 4 | Jasmine | 77 | | 3 | Steve | 74 | | 6 | Luis | 1 | | 8 | Jonathan | 7 | | 7 | Daiana | 33 | | 11 | Madelynn | 1 | +------+----------+---------------+結(jié)果表: +------+----------+ | id | name | +------+----------+ | 2 | John | | 7 | Daiana | | 4 | Jasmine | | 3 | Steve | +------+----------+John, Daiana, Steve 和 Jasmine 所在的院系 分別是 14, 33, 74 和 77, 其中 14, 33, 74 和 77 并不存在于院系表來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/students-with-invalid-departments
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select c.id, c.name from (select a.id, a.name, b.name depart_namefrom Students a left join Departments bon b.id = a.department_id ) c where c.depart_name is null # 笛卡爾積,不存在的院系name會為null select a.id, a.name from Students a left join Departments b on b.id = a.department_id where b.id is null # 笛卡爾積,不存在的院系id會為nullor
# Write your MySQL query statement below select a.id, a.name from Students a where a.department_id not in (select distinct id from Departments )練習(xí) LeetCode 613. 直線上的最近距離
表 point 保存了一些點(diǎn)在 x 軸上的坐標(biāo),這些坐標(biāo)都是整數(shù)。
寫一個(gè)查詢語句,找到這些點(diǎn)中最近兩個(gè)點(diǎn)之間的距離。
| x | |-----| | -1 | | 0 | | 2 |最近距離顯然是 ‘1’ ,是點(diǎn) ‘-1’ 和 ‘0’ 之間的距離。所以輸出應(yīng)該如下:
| shortest| |---------| | 1 |注意:每個(gè)點(diǎn)都與其他點(diǎn)坐標(biāo)不同,表 table 不會有重復(fù)坐標(biāo)出現(xiàn)。
進(jìn)階:如果這些點(diǎn)在 x 軸上從左到右都有一個(gè)編號,輸出結(jié)果時(shí)需要輸出最近點(diǎn)對的編號呢?
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/shortest-distance-in-a-line
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select min(abs(p1.x - p2.x)) shortest from point p1 left join point p2 on p1.x != p2.x練習(xí) LeetCode 1251. 平均售價(jià)
Table: Prices
+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | start_date | date | | end_date | date | | price | int | +---------------+---------+(product_id,start_date,end_date) 是 Prices 表的主鍵。
Prices 表的每一行表示的是某個(gè)產(chǎn)品在一段時(shí)期內(nèi)的價(jià)格。
每個(gè)產(chǎn)品的對應(yīng)時(shí)間段是不會重疊的,這也意味著同一個(gè)產(chǎn)品的價(jià)格時(shí)段不會出現(xiàn)交叉。
Table: UnitsSold
+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | purchase_date | date | | units | int | +---------------+---------+UnitsSold 表沒有主鍵,它可能包含重復(fù)項(xiàng)。
UnitsSold 表的每一行表示的是每種產(chǎn)品的出售日期,單位和產(chǎn)品 id。
編寫SQL查詢以查找每種產(chǎn)品的平均售價(jià)。
average_price 應(yīng)該四舍五入到小數(shù)點(diǎn)后兩位。
查詢結(jié)果格式如下例所示:
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/average-selling-price
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select p.product_id, round(sum(p.price*s.units)/sum(s.units), 2) average_price from Prices p left join UnitsSold s on p.product_id = s.product_id and s.purchase_date >= p.start_dateand s.purchase_date <= p.end_date group by p.product_id練習(xí) LeetCode 577. 員工獎(jiǎng)金
選出所有 bonus < 1000 的員工的 name 及其 bonus。
Employee 表單
+-------+--------+-----------+--------+ | empId | name | supervisor| salary | +-------+--------+-----------+--------+ | 1 | John | 3 | 1000 | | 2 | Dan | 3 | 2000 | | 3 | Brad | null | 4000 | | 4 | Thomas | 3 | 4000 | +-------+--------+-----------+--------+empId 是這張表單的主關(guān)鍵字
Bonus 表單
+-------+-------+ | empId | bonus | +-------+-------+ | 2 | 500 | | 4 | 2000 | +-------+-------+empId 是這張表單的主關(guān)鍵字
輸出示例:
+-------+-------+ | name | bonus | +-------+-------+ | John | null | | Dan | 500 | | Brad | null | +-------+-------+來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/employee-bonus
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select e.name, b.bonus from Employee e left join Bonus b using (empId) where b.bonus < 1000 or b.bonus is null練習(xí) LeetCode 1327. 列出指定時(shí)間段內(nèi)所有的下單產(chǎn)品
表: Products
+------------------+---------+ | Column Name | Type | +------------------+---------+ | product_id | int | | product_name | varchar | | product_category | varchar | +------------------+---------+product_id 是該表主鍵。
該表包含該公司產(chǎn)品的數(shù)據(jù)。
表: Orders
+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | order_date | date | | unit | int | +---------------+---------+該表無主鍵,可能包含重復(fù)行。
product_id 是表單 Products 的外鍵。
unit 是在日期 order_date 內(nèi)下單產(chǎn)品的數(shù)目。
寫一個(gè) SQL 語句,要求獲取在 2020 年 2 月份下單的數(shù)量不少于 100 的產(chǎn)品的名字和數(shù)目。
返回結(jié)果表單的順序無要求。
查詢結(jié)果的格式如下:
Products 表: +-------------+-----------------------+------------------+ | product_id | product_name | product_category | +-------------+-----------------------+------------------+ | 1 | Leetcode Solutions | Book | | 2 | Jewels of Stringology | Book | | 3 | HP | Laptop | | 4 | Lenovo | Laptop | | 5 | Leetcode Kit | T-shirt | +-------------+-----------------------+------------------+Orders 表: +--------------+--------------+----------+ | product_id | order_date | unit | +--------------+--------------+----------+ | 1 | 2020-02-05 | 60 | | 1 | 2020-02-10 | 70 | | 2 | 2020-01-18 | 30 | | 2 | 2020-02-11 | 80 | | 3 | 2020-02-17 | 2 | | 3 | 2020-02-24 | 3 | | 4 | 2020-03-01 | 20 | | 4 | 2020-03-04 | 30 | | 4 | 2020-03-04 | 60 | | 5 | 2020-02-25 | 50 | | 5 | 2020-02-27 | 50 | | 5 | 2020-03-01 | 50 | +--------------+--------------+----------+Result 表: +--------------------+---------+ | product_name | unit | +--------------------+---------+ | Leetcode Solutions | 130 | | Leetcode Kit | 100 | +--------------------+---------+2020 年 2 月份下單 product_id = 1 的產(chǎn)品的數(shù)目總和為 (60 + 70) = 130 。 2020 年 2 月份下單 product_id = 2 的產(chǎn)品的數(shù)目總和為 80 。 2020 年 2 月份下單 product_id = 3 的產(chǎn)品的數(shù)目總和為 (2 + 3) = 5 。 2020 年 2 月份 product_id = 4 的產(chǎn)品并沒有下單。 2020 年 2 月份下單 product_id = 5 的產(chǎn)品的數(shù)目總和為 (50 + 50) = 100 。來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/list-the-products-ordered-in-a-period
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select product_name, unit from (select product_id, sum(unit) unit from Orderswhere(order_date >= '2020-02-01' and order_date < '2020-03-01')group by product_id ) t left join Products using(product_id) where unit >= 100練習(xí) LeetCode 603. 連續(xù)空余座位
幾個(gè)朋友來到電影院的售票處,準(zhǔn)備預(yù)約連續(xù)空余座位。
你能利用表 cinema ,幫他們寫一個(gè)查詢語句,獲取所有空余座位,并將它們按照 seat_id 排序后返回嗎?
| seat_id | free | |---------|------| | 1 | 1 | | 2 | 0 | | 3 | 1 | | 4 | 1 | | 5 | 1 |對于如上樣例,你的查詢語句應(yīng)該返回如下結(jié)果。
| seat_id | |---------| | 3 | | 4 | | 5 |注意:
seat_id 字段是一個(gè)自增的整數(shù),free 字段是布爾類型(‘1’ 表示空余, ‘0’ 表示已被占據(jù))。
連續(xù)空余座位的定義是大于等于 2 個(gè)連續(xù)空余的座位。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/consecutive-available-seats
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select distinct a.seat_idfrom cinema a join cinema bon abs(a.seat_id-b.seat_id) = 1 and a.free = true and b.free = true order by a.seat_id練習(xí) LeetCode 607. 銷售員
給定 3 個(gè)表: salesperson, company, orders。
輸出所有表 salesperson 中,沒有向公司 ‘RED’ 銷售任何東西的銷售員。
來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/sales-person
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select name from salesperson where sales_id not in (select sales_id fromorders left join companyusing(com_id)where company.name = 'RED' )練習(xí) LeetCode 1294. 不同國家的天氣類型
國家表:Countries
+---------------+---------+ | Column Name | Type | +---------------+---------+ | country_id | int | | country_name | varchar | +---------------+---------+ country_id 是這張表的主鍵。 該表的每行有 country_id 和 country_name 兩列。天氣表:Weather
+---------------+---------+ | Column Name | Type | +---------------+---------+ | country_id | int | | weather_state | varchar | | day | date | +---------------+---------+ (country_id, day) 是該表的復(fù)合主鍵。 該表的每一行記錄了某個(gè)國家某一天的天氣情況。寫一段 SQL 來找到表中每個(gè)國家在 2019 年 11 月的天氣類型。
天氣類型的定義如下:
當(dāng) weather_state 的平均值小于或等于15返回 Cold,
當(dāng) weather_state 的平均值大于或等于 25 返回 Hot,
否則返回 Warm。
你可以以任意順序返回你的查詢結(jié)果。
查詢結(jié)果格式如下所示:
Countries table: +------------+--------------+ | country_id | country_name | +------------+--------------+ | 2 | USA | | 3 | Australia | | 7 | Peru | | 5 | China | | 8 | Morocco | | 9 | Spain | +------------+--------------+ Weather table: +------------+---------------+------------+ | country_id | weather_state | day | +------------+---------------+------------+ | 2 | 15 | 2019-11-01 | | 2 | 12 | 2019-10-28 | | 2 | 12 | 2019-10-27 | | 3 | -2 | 2019-11-10 | | 3 | 0 | 2019-11-11 | | 3 | 3 | 2019-11-12 | | 5 | 16 | 2019-11-07 | | 5 | 18 | 2019-11-09 | | 5 | 21 | 2019-11-23 | | 7 | 25 | 2019-11-28 | | 7 | 22 | 2019-12-01 | | 7 | 20 | 2019-12-02 | | 8 | 25 | 2019-11-05 | | 8 | 27 | 2019-11-15 | | 8 | 31 | 2019-11-25 | | 9 | 7 | 2019-10-23 | | 9 | 3 | 2019-12-23 | +------------+---------------+------------+ Result table: +--------------+--------------+ | country_name | weather_type | +--------------+--------------+ | USA | Cold | | Austraila | Cold | | Peru | Hot | | China | Warm | | Morocco | Hot | +--------------+--------------+ USA 11 月的平均 weather_state 為 (15) / 1 = 15 所以天氣類型為 Cold。 Australia 11 月的平均 weather_state 為 (-2 + 0 + 3) / 3 = 0.333 所以天氣類型為 Cold。 Peru 11 月的平均 weather_state 為 (25) / 1 = 25 所以天氣類型為 Hot。 China 11 月的平均 weather_state 為 (16 + 18 + 21) / 3 = 18.333 所以天氣類型為 Warm。 Morocco 11 月的平均 weather_state 為 (25 + 27 + 31) / 3 = 27.667 所以天氣類型為 Hot。 我們并不知道 Spain 在 11 月的 weather_state 情況所以無需將他包含在結(jié)果中。來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/weather-type-in-each-country
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
解題:
# Write your MySQL query statement below select country_name, case when avg(weather_state)<= 15 then 'Cold'when avg(weather_state)>= 25 then 'Hot'else 'Warm' end as weather_type from Countries left join Weather using (country_id) # where day between '2019-11-01' and '2019-11-30' where day like '2019-11%' # 也可以 group by country_id總結(jié)
以上是生活随笔為你收集整理的MySQL 多表查询、连接查询(内连接、外连接)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 742. 二叉树最近的
- 下一篇: linux cmake编译源码,linu