MySQL 基本查询、条件查询、投影查询
文章目錄
- 1. 基本查詢
- 2. 條件查詢
- 3. 投影查詢
- 練習 LeetCode 595. 大的國家
- 練習 LeetCode 584. 尋找用戶推薦人
- 練習 LeetCode 1173. 即時食物配送 I
- 練習 LeetCode 610. 判斷三角形
學習自 廖雪峰的官方網站
1. 基本查詢
SELECT * FROM <表名>*表示所有內容
許多檢測工具會執行一條SELECT 1; 來測試數據庫連接。
2. 條件查詢
SELECT * FROM <表名> WHERE <條件表達式>條件運算按照NOT、AND、OR的優先級進行,即 NOT 最高,其次AND,最后OR
加括號 可以改變 優先級
3. 投影查詢
結果集僅包含指定列
SELECT 列1, 列2, 列3 FROM <表名> WHERE <條件> SELECT id, score, name FROM students;# 下面的 score 改了名稱為 points(重命名) SELECT id, score points, name FROM students;練習 LeetCode 595. 大的國家
題目:
Create table If Not Exists World (name varchar(255), continent varchar(255), area int, population int, gdp int) Truncate table World insert into World (name, continent, area, population, gdp) values ('Afghanistan', 'Asia', '652230', '25500100', '20343000000') insert into World (name, continent, area, population, gdp) values ('Albania', 'Europe', '28748', '2831741', '12960000000') insert into World (name, continent, area, population, gdp) values ('Algeria', 'Africa', '2381741', '37100000', '188681000000') insert into World (name, continent, area, population, gdp) values ('Andorra', 'Europe', '468', '78115', '3712000000') insert into World (name, continent, area, population, gdp) values ('Angola', 'Africa', '1246700', '20609294', '100990000000')這里有張 World 表
+-----------------+------------+------------+--------------+---------------+ | name | continent | area | population | gdp | +-----------------+------------+------------+--------------+---------------+ | Afghanistan | Asia | 652230 | 25500100 | 20343000 | | Albania | Europe | 28748 | 2831741 | 12960000 | | Algeria | Africa | 2381741 | 37100000 | 188681000 | | Andorra | Europe | 468 | 78115 | 3712000 | | Angola | Africa | 1246700 | 20609294 | 100990000 | +-----------------+------------+------------+--------------+---------------+如果一個國家的面積超過300萬平方公里,或者人口超過2500萬,那么這個國家就是大國家。
編寫一個SQL查詢,輸出表中所有大國家的名稱、人口和面積。
例如,根據上表,我們應該輸出:
+--------------+-------------+--------------+ | name | population | area | +--------------+-------------+--------------+ | Afghanistan | 25500100 | 652230 | | Algeria | 37100000 | 2381741 | +--------------+-------------+--------------+來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/big-countries
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
解題:
# Write your MySQL query statement below SELECT name, population, area FROM World WHERE population > 25000000 OR area > 3000000;格式無特殊要求,好像
# Write your MySQL query statement below SELECT name, population, area FROM World WHERE population > 25000000 OR area > 3000000;217 ms
練習 LeetCode 584. 尋找用戶推薦人
給定表 customer ,里面保存了所有客戶信息和他們的推薦人。
+------+------+-----------+ | id | name | referee_id| +------+------+-----------+ | 1 | Will | NULL | | 2 | Jane | NULL | | 3 | Alex | 2 | | 4 | Bill | NULL | | 5 | Zack | 1 | | 6 | Mark | 2 | +------+------+-----------+寫一個查詢語句,返回一個編號列表,列表中編號的推薦人的編號都 不是 2。
對于上面的示例數據,結果為:
+------+ | name | +------+ | Will | | Jane | | Bill | | Zack | +------+來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/find-customer-referee
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
解題:
# Write your MySQL query statement below select name from customer where referee_id not in (2) or referee_id is null # Write your MySQL query statement below select name from customer where referee_id != 2 or referee_id is null # Write your MySQL query statement below select name from customer where referee_id <> 2 or referee_id is null練習 LeetCode 1173. 即時食物配送 I
配送表: Delivery
+-----------------------------+---------+ | Column Name | Type | +-----------------------------+---------+ | delivery_id | int | | customer_id | int | | order_date | date | | customer_pref_delivery_date | date | +-----------------------------+---------+delivery_id 是表的主鍵。
該表保存著顧客的食物配送信息,顧客在某個日期下了訂單,并指定了一個期望的配送日期(和下單日期相同或者在那之后)。
如果顧客期望的配送日期和下單日期相同,則該訂單稱為 「即時訂單」,否則稱為「計劃訂單」。
寫一條 SQL 查詢語句獲取即時訂單所占的比例, 保留兩位小數。
查詢結果如下所示:
Delivery 表: +-------------+-------------+------------+-----------------------------+ | delivery_id | customer_id | order_date | customer_pref_delivery_date | +-------------+-------------+------------+-----------------------------+ | 1 | 1 | 2019-08-01 | 2019-08-02 | | 2 | 5 | 2019-08-02 | 2019-08-02 | | 3 | 1 | 2019-08-11 | 2019-08-11 | | 4 | 3 | 2019-08-24 | 2019-08-26 | | 5 | 4 | 2019-08-21 | 2019-08-22 | | 6 | 2 | 2019-08-11 | 2019-08-13 | +-------------+-------------+------------+-----------------------------+Result 表: +----------------------+ | immediate_percentage | +----------------------+ | 33.33 | +----------------------+ 2 和 3 號訂單為即時訂單,其他的為計劃訂單。來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/immediate-food-delivery-i
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
解題:
# Write your MySQL query statement below select round((sum(order_date = customer_pref_delivery_date)/count(*)*100), 2) immediate_percentage from Deliveryavg 函數返回占比
# Write your MySQL query statement below select round((avg(order_date = customer_pref_delivery_date)*100), 2) immediate_percentage from Delivery練習 LeetCode 610. 判斷三角形
一個小學生 Tim 的作業是判斷三條線段是否能形成一個三角形。
然而,這個作業非常繁重,因為有幾百組線段需要判斷。
假設表 triangle 保存了所有三條線段的三元組 x, y, z ,
你能幫 Tim 寫一個查詢語句,來判斷每個三元組是否可以組成一個三角形嗎?
對于如上樣例數據,你的查詢語句應該返回如下結果:
| x | y | z | triangle | |----|----|----|----------| | 13 | 15 | 30 | No | | 10 | 20 | 15 | Yes |來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/triangle-judgement
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
解題:
# Write your MySQL query statement below select x, y, z, case when x+y > z and x+z > y and y+z > x then 'Yes'else 'No'end as triangle from triangleor
# Write your MySQL query statement below select *, if(x+y>z and x+z>y and y+z>x, 'Yes', 'No') as triangle from triangle總結
以上是生活随笔為你收集整理的MySQL 基本查询、条件查询、投影查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 1186. 删除一次得
- 下一篇: LeetCode 734. 句子相似性(