日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

MySQL 多表查询、连接查询(内连接、外连接)

發(fā)布時間:2024/7/5 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL 多表查询、连接查询(内连接、外连接) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 1. 多表查詢
    • 2. 連接查詢
    • 練習 LeetCode 175. 組合兩個表
    • 練習 LeetCode 181. 超過經理收入的員工
    • 練習 LeetCode 1378. 使用唯一標識碼替換員工ID
    • 練習 LeetCode 1068. 產品銷售分析 I
    • 練習 LeetCode 1069. 產品銷售分析 II
    • 練習 LeetCode 1303. 求團隊人數(shù)
    • 練習 LeetCode 1350. 院系無效的學生
    • 練習 LeetCode 613. 直線上的最近距離
    • 練習 LeetCode 1251. 平均售價
    • 練習 LeetCode 577. 員工獎金
    • 練習 LeetCode 1327. 列出指定時間段內所有的下單產品
    • 練習 LeetCode 603. 連續(xù)空余座位
    • 練習 LeetCode 607. 銷售員
    • 練習 LeetCode 1294. 不同國家的天氣類型

學習自 廖雪峰的官方網(wǎng)站

1. 多表查詢

SELECT * FROM <1> <2> SELECT * FROM students, classes;

查詢的結果是一個二維表,它是students表和classes表的“乘積”,即students表的每一行與classes表的每一行都兩兩拼在一起返回

結果集的列數(shù)是兩表的列數(shù)之和,行數(shù)是兩表行數(shù)之積(要小心,乘積有可能很大)。

  • 對一樣的列名,起別名區(qū)分(下面的name、id)
SELECTstudents.id sid,students.name,students.gender,students.score,classes.id cid,classes.name cname FROM students, classes;
  • 表也可以起別名,方便簡寫
SELECTs.id sid,s.name,s.gender,s.score,c.id cid,c.name cname FROM students s, classes c;
  • 多表查詢也可以使用WHERE條件
SELECTs.id sid,s.name,s.gender,s.score,c.id cid,c.name cname FROM students s, classes c WHERE s.gender = 'M' AND c.id = 1; 1班的男生

2. 連接查詢

連接查詢是另一種類型的多表查詢。

連接查詢對多個表進行JOIN運算:

  • 先確定一個主表作為結果集
  • 然后,把其他表的行有選擇性“連接”在主表結果集上
選出所有學生的信息 SELECT s.id, s.name, s.class_id, s.gender, s.score FROM students s; 我們還需要班級的 名稱
  • 最常用的一種內連接——INNER JOIN來實現(xiàn)
SELECT s.id, s.name, s.class_id, c.name class_name, s.gender, s.score FROM students s 主表 INNER JOIN classes c 需要連接的表 ON s.class_id = c.id; ON 條件 可選:加上WHERE子句、ORDER BY等子句
  • 外連接 LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN’

  • 區(qū)別:哪邊的表的數(shù)據(jù)完全保留,另一個表的數(shù)據(jù)不存在的填NULL

SELECT ... FROM tableA ??? JOIN tableB ON tableA.column1 = tableB.column2;

練習 LeetCode 175. 組合兩個表

題目:

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 是上表主鍵編寫一個 SQL 查詢,滿足條件:無論 person 是否有地址信息, 都需要基于上述兩表提供 person 的以下信息:FirstName, LastName, City, State

來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/combine-two-tables
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。

解題:

# 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.PersonId

369 ms

練習 LeetCode 181. 超過經理收入的員工

題目:

Employee 表包含所有員工,他們的經理也屬于員工。每個員工都有一個 Id,此外還有一列對應員工的經理的 Id。

+----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+

給定 Employee 表,編寫一個 SQL 查詢,該查詢可以獲取收入超過他們經理的員工的姓名。在上面的表格中,Joe 是唯一一個收入超過他的經理的員工。

+----------+ | Employee | +----------+ | Joe | +----------+

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。

解題:
把同一份表再次JOIN該表,條件是A.ManagerId = B.Id

# Write your MySQL query statement below SELECT A.Name Employee FROM Employee A LEFT OUTER JOIN Employee B ON A.ManagerId = B.Id WHERE A.Salary > B.Salary

或者

# 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.Salary

309 ms

練習 LeetCode 1378. 使用唯一標識碼替換員工ID

Employees 表:

+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+

id 是這張表的主鍵。
這張表的每一行分別代表了某公司其中一位員工的名字和 ID 。

EmployeeUNI 表:

+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | unique_id | int | +---------------+---------+

(id, unique_id) 是這張表的主鍵。
這張表的每一行包含了該公司某位員工的 ID 和他的唯一標識碼(unique ID)。

寫一段SQL查詢來展示每位用戶的 唯一標識碼(unique ID );如果某位員工沒有唯一標識碼,使用 null 填充即可。

你可以以 任意 順序返回結果表。

查詢結果的格式如下例所示:

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 沒有唯一標識碼, 因此我們使用 null 替代。 Meir 的唯一標識碼是 2 。 Winston 的唯一標識碼是 3 。 Jonathan 唯一標識碼是 1

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/replace-employee-id-with-the-unique-identifier
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# Write your MySQL query statement below select a.unique_id, b.name from Employees b left join EmployeeUNI a on a.id = b.id

練習 LeetCode 1068. 產品銷售分析 I

銷售表 Sales:

+-------------+-------+ | Column Name | Type | +-------------+-------+ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | +-------------+-------+

(sale_id, year) 是銷售表 Sales 的主鍵.
product_id 是產品表 Product 的外鍵.
注意: price 表示每單位價格

產品表 Product:

+--------------+---------+ | Column Name | Type | +--------------+---------+ | product_id | int | | product_name | varchar | +--------------+---------+

product_id 是表的主鍵.
寫一條SQL 查詢語句獲取產品表 Product 中所有的 產品名稱 product name 以及 該產品在 Sales 表中相對應的 上市年份 year 和 價格 price。

示例: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_name | year | price | +--------------+-------+-------+ | Nokia | 2008 | 5000 | | Nokia | 2009 | 5000 | | Apple | 2011 | 9000 | +--------------+-------+-------+

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/product-sales-analysis-i
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# 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

練習 LeetCode 1069. 產品銷售分析 II

銷售表:Sales

+-------------+-------+ | Column Name | Type | +-------------+-------+ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | +-------------+-------+

sale_id 是這個表的主鍵。
product_id 是 Product 表的外鍵。
請注意價格是每單位的。
產品表:Product

+--------------+---------+ | Column Name | Type | +--------------+---------+ | product_id | int | | product_name | varchar | +--------------+---------+

product_id 是這個表的主鍵。

編寫一個 SQL 查詢,按產品 id product_id 來統(tǒng)計每個產品的銷售總量。

查詢結果格式如下面例子所示:

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
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# 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 # 可選排序

練習 LeetCode 1303. 求團隊人數(shù)

員工表:Employee

+---------------+---------+ | Column Name | Type | +---------------+---------+ | employee_id | int | | team_id | int | +---------------+---------+

employee_id 字段是這張表的主鍵,表中的每一行都包含每個員工的 ID 和他們所屬的團隊。
編寫一個 SQL 查詢,以求得每個員工所在團隊的總人數(shù)。

查詢結果中的順序無特定要求。

查詢結果格式示例如下:

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 的團隊的成員, ID 為 4 的員工是 team_id 為 7 的團隊的成員, ID 為 5、6 的員工是 team_id 為 9 的團隊的成員。

來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/find-the-team-size
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

  • 自連接,按著 team_id,有重復的 team_id,產生了笛卡爾積
# Write your MySQL query statement below select a.employee_id, count(*) team_size from Employee a left join Employee b on a.team_id = b.team_id # 產生了笛卡爾積 group by a.employee_id

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)

練習 LeetCode 1350. 院系無效的學生

院系表: Departments

+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+

id 是該表的主鍵
該表包含一所大學每個院系的 id 信息

學生表: Students

+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | | department_id | int | +---------------+---------+

id 是該表的主鍵
該表包含一所大學每個學生的 id 和他/她就讀的院系信息

寫一條 SQL 語句以查詢那些所在院系不存在的學生的 id 和姓名

可以以任何順序返回結果

下面是返回結果格式的例子

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 | +------+----------+---------------+結果表: +------+----------+ | id | name | +------+----------+ | 2 | John | | 7 | Daiana | | 4 | Jasmine | | 3 | Steve | +------+----------+John, Daiana, Steve 和 Jasmine 所在的院系 分別是 14, 33, 7477, 其中 14, 33, 7477 并不存在于院系表

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/students-with-invalid-departments
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# 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會為null

or

# 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 )

練習 LeetCode 613. 直線上的最近距離

表 point 保存了一些點在 x 軸上的坐標,這些坐標都是整數(shù)。

寫一個查詢語句,找到這些點中最近兩個點之間的距離。

| x | |-----| | -1 | | 0 | | 2 |

最近距離顯然是 ‘1’ ,是點 ‘-1’ 和 ‘0’ 之間的距離。所以輸出應該如下:

| shortest| |---------| | 1 |

注意:每個點都與其他點坐標不同,表 table 不會有重復坐標出現(xiàn)。

進階:如果這些點在 x 軸上從左到右都有一個編號,輸出結果時需要輸出最近點對的編號呢?

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/shortest-distance-in-a-line
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# 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

練習 LeetCode 1251. 平均售價

Table: Prices

+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | start_date | date | | end_date | date | | price | int | +---------------+---------+

(product_id,start_date,end_date) 是 Prices 表的主鍵。
Prices 表的每一行表示的是某個產品在一段時期內的價格。
每個產品的對應時間段是不會重疊的,這也意味著同一個產品的價格時段不會出現(xiàn)交叉。

Table: UnitsSold

+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | purchase_date | date | | units | int | +---------------+---------+

UnitsSold 表沒有主鍵,它可能包含重復項。
UnitsSold 表的每一行表示的是每種產品的出售日期,單位和產品 id。

編寫SQL查詢以查找每種產品的平均售價。
average_price 應該四舍五入到小數(shù)點后兩位。
查詢結果格式如下例所示:

Prices table: +------------+------------+------------+--------+ | product_id | start_date | end_date | price | +------------+------------+------------+--------+ | 1 | 2019-02-17 | 2019-02-28 | 5 | | 1 | 2019-03-01 | 2019-03-22 | 20 | | 2 | 2019-02-01 | 2019-02-20 | 15 | | 2 | 2019-02-21 | 2019-03-31 | 30 | +------------+------------+------------+--------+UnitsSold table: +------------+---------------+-------+ | product_id | purchase_date | units | +------------+---------------+-------+ | 1 | 2019-02-25 | 100 | | 1 | 2019-03-01 | 15 | | 2 | 2019-02-10 | 200 | | 2 | 2019-03-22 | 30 | +------------+---------------+-------+Result table: +------------+---------------+ | product_id | average_price | +------------+---------------+ | 1 | 6.96 | | 2 | 16.96 | +------------+---------------+ 平均售價 = 產品總價 / 銷售的產品數(shù)量。 產品 1 的平均售價 = ((100 * 5)+(15 * 20) )/ 115 = 6.96 產品 2 的平均售價 = ((200 * 15)+(30 * 30) )/ 230 = 16.96

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/average-selling-price
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# 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

練習 LeetCode 577. 員工獎金

選出所有 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 是這張表單的主關鍵字

Bonus 表單

+-------+-------+ | empId | bonus | +-------+-------+ | 2 | 500 | | 4 | 2000 | +-------+-------+

empId 是這張表單的主關鍵字

輸出示例:

+-------+-------+ | name | bonus | +-------+-------+ | John | null | | Dan | 500 | | Brad | null | +-------+-------+

來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/employee-bonus
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# 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

練習 LeetCode 1327. 列出指定時間段內所有的下單產品

表: Products

+------------------+---------+ | Column Name | Type | +------------------+---------+ | product_id | int | | product_name | varchar | | product_category | varchar | +------------------+---------+

product_id 是該表主鍵。
該表包含該公司產品的數(shù)據(jù)。

表: Orders

+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | order_date | date | | unit | int | +---------------+---------+

該表無主鍵,可能包含重復行。
product_id 是表單 Products 的外鍵。
unit 是在日期 order_date 內下單產品的數(shù)目。

寫一個 SQL 語句,要求獲取在 2020 年 2 月份下單的數(shù)量不少于 100 的產品的名字和數(shù)目。

返回結果表單的順序無要求。

查詢結果的格式如下:

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 | +--------------------+---------+20202 月份下單 product_id = 1 的產品的數(shù)目總和為 (60 + 70) = 13020202 月份下單 product_id = 2 的產品的數(shù)目總和為 80 。 20202 月份下單 product_id = 3 的產品的數(shù)目總和為 (2 + 3) = 5 。 20202 月份 product_id = 4 的產品并沒有下單。 20202 月份下單 product_id = 5 的產品的數(shù)目總和為 (50 + 50) = 100 。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/list-the-products-ordered-in-a-period
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# 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

練習 LeetCode 603. 連續(xù)空余座位

幾個朋友來到電影院的售票處,準備預約連續(xù)空余座位。

你能利用表 cinema ,幫他們寫一個查詢語句,獲取所有空余座位,并將它們按照 seat_id 排序后返回嗎?

| seat_id | free | |---------|------| | 1 | 1 | | 2 | 0 | | 3 | 1 | | 4 | 1 | | 5 | 1 |

對于如上樣例,你的查詢語句應該返回如下結果。

| seat_id | |---------| | 3 | | 4 | | 5 |

注意:
seat_id 字段是一個自增的整數(shù),free 字段是布爾類型(‘1’ 表示空余, ‘0’ 表示已被占據(jù))。
連續(xù)空余座位的定義是大于等于 2 個連續(xù)空余的座位。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/consecutive-available-seats
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# 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

練習 LeetCode 607. 銷售員

給定 3 個表: salesperson, company, orders。
輸出所有表 salesperson 中,沒有向公司 ‘RED’ 銷售任何東西的銷售員。

示例: 輸入表: salesperson +----------+------+--------+-----------------+-----------+ | sales_id | name | salary | commission_rate | hire_date | +----------+------+--------+-----------------+-----------+ | 1 | John | 100000 | 6 | 4/1/2006 | | 2 | Amy | 120000 | 5 | 5/1/2010 | | 3 | Mark | 65000 | 12 | 12/25/2008| | 4 | Pam | 25000 | 25 | 1/1/2005 | | 5 | Alex | 50000 | 10 | 2/3/2007 | +----------+------+--------+-----------------+-----------+ 表 salesperson 存儲了所有銷售員的信息。 每個銷售員都有一個銷售員編號 sales_id 和他的名字 name 。表: company +---------+--------+------------+ | com_id | name | city | +---------+--------+------------+ | 1 | RED | Boston | | 2 | ORANGE | New York | | 3 | YELLOW | Boston | | 4 | GREEN | Austin | +---------+--------+------------+ 表 company 存儲了所有公司的信息。 每個公司都有一個公司編號 com_id 和它的名字 name 。表: orders +----------+------------+---------+----------+--------+ | order_id | order_date | com_id | sales_id | amount | +----------+------------+---------+----------+--------+ | 1 | 1/1/2014 | 3 | 4 | 100000 | | 2 | 2/1/2014 | 4 | 5 | 5000 | | 3 | 3/1/2014 | 1 | 1 | 50000 | | 4 | 4/1/2014 | 1 | 4 | 25000 | +----------+----------+---------+----------+--------+ 表 orders 存儲了所有的銷售數(shù)據(jù), 包括銷售員編號 sales_id 和公司編號 com_id 。輸出 +------+ | name | +------+ | Amy | | Mark | | Alex | +------+ 解釋 根據(jù)表 orders 中的訂單 '3''4' , 容易看出只有 'John''Pam' 兩個銷售員曾經向公司 'RED' 銷售過。所以我們需要輸出表 salesperson 中所有其他人的名字。

來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/sales-person
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# 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' )

練習 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) 是該表的復合主鍵。 該表的每一行記錄了某個國家某一天的天氣情況。

寫一段 SQL 來找到表中每個國家在 2019 年 11 月的天氣類型。

天氣類型的定義如下:
當 weather_state 的平均值小于或等于15返回 Cold,
當 weather_state 的平均值大于或等于 25 返回 Hot,
否則返回 Warm。

你可以以任意順序返回你的查詢結果。

查詢結果格式如下所示:

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 情況所以無需將他包含在結果中。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/weather-type-in-each-country
著作權歸領扣網(wǎng)絡所有。商業(yè)轉載請聯(lián)系官方授權,非商業(yè)轉載請注明出處。


解題:

# 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

總結

以上是生活随笔為你收集整理的MySQL 多表查询、连接查询(内连接、外连接)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。