当前位置:网站首页>【力扣10天SQL入门】Day4 组合查询 & 指定选取
【力扣10天SQL入门】Day4 组合查询 & 指定选取
2022-06-28 08:18:00 【ly甲烷】
1965.丢失信息的雇员
表: Employees
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| name | varchar |
+-------------+---------+
employee_id 是这个表的主键。每一行表示雇员的id 和他的姓名。
表: Salaries
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| employee_id | int |
| salary | int |
+-------------+---------+
employee_id is 这个表的主键。每一行表示雇员的id 和他的薪水。
写出一个查询语句,找到所有 雇员的 姓名 丢失了,或者雇员的 薪水信息 丢失了的雇员id。
返回这些雇员的id employee_id , 从小到大排序 。
答案解析
联表查询,全连接,但是mysql不值钱完全外连接, 所有可以有左连接和有连接拼起来
Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
SELECT A.employee_id FROM employees A LEFT JOIN salaries B ON A.employee_id = B.employee_id
WHERE B.salary IS NULL
UNION
SELECT B.employee_id FROM employees A RIGHT JOIN salaries B ON A.employee_id = B.employee_id
WHERE A.name IS NULL
ORDER BY employee_id
1795.每个产品在不同商店的价格
表:Products
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| store1 | int |
| store2 | int |
| store3 | int |
+-------------+---------+
这张表的主键是product_id(产品Id)。
每行存储了这一产品在不同商店store1, store2, store3的价格。如果这一产品在商店里没有出售,则值将为null。
请你重构 Products 表,查询每个产品在不同商店的价格,
使得输出的格式变为(product_id, store, price) 。如果这一产品在商店里没有出售,则不输出这一行。
输出结果表中的 顺序不作要求 。
输入:
Products table:
+------------+--------+--------+--------+
| product_id | store1 | store2 | store3 |
+------------+--------+--------+--------+
| 0 | 95 | 100 | 105 |
| 1 | 70 | null | 80 |
+------------+--------+--------+--------+
输出:
+------------+--------+-------+
| product_id | store | price |
+------------+--------+-------+
| 0 | store1 | 95 |
| 0 | store2 | 100 |
| 0 | store3 | 105 |
| 1 | store1 | 70 |
| 1 | store3 | 80 |
+------------+--------+-------+
答案解析
行转列用groupby,列转行用union all
union all 和 union 区别是 union all 不去重
sumif
SELECT product_id, 'store1' store,store1 price
FROM products
WHERE store1 IS NOT NULL
UNION ALL
SELECT product_id, 'store2' store,store2 price
FROM products
WHERE store2 IS NOT NULL
UNION ALL
SELECT product_id, 'store3' store,store3 store
FROM products
WHERE store3 IS NOT NULL
608.树节点
给定一个表 tree,id 是树节点的编号, p_id 是它父节点的 id 。
+----+------+
| id | p_id |
+----+------+
| 1 | null |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
+----+------+
树中每个节点属于以下三种类型之一:
-叶子:如果这个节点没有任何孩子节点。
--根:如果这个节点是整棵树的根,即没有父节点。
--内部节点:如果这个节点既不是叶子节点也不是根节点。
写一个查询语句,输出所有节点的编号和节点的类型,并将结果按照节点编号排序。上面样例的结果为:
+----+------+
| id | Type |
+----+------+
| 1 | Root |
| 2 | Inner|
| 3 | Leaf |
| 4 | Leaf |
| 5 | Leaf |
+----+------+
答案解析
not in与null值, 如果not in 集合里有null值,那直接返回null 对应结果为false
简单的case语句的几种用法
CASE WHEN condition THEN result ELSE result END
CASE WHEN p_id IS NULL THEN 'Root'
WHEN id NOT IN(SELECT p_id FROM tree WHERE p_id IS NOT NULL) THEN 'Leaf' ELSE 'Inner' END
CASE SCORE WHEN 'A' THEN '优' ELSE '不及格' END
答案:
SELECT
id,
CASE WHEN p_id IS NULL THEN 'Root'
WHEN id NOT IN(SELECT p_id FROM tree WHERE p_id IS NOT NULL) THEN 'Leaf' ELSE 'Inner' END AS 'type'
FROM tree
176.第二高的薪水
Employee 表:
+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| salary | int |
+-------------+------+
id 是这个表的主键。
表的每一行包含员工的工资信息。
编写一个 SQL 查询,获取并返回 Employee 表中第二高的薪水 。如果不存在第二高的薪水,查询应该返回 null 。
输入:
Employee 表:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
输出:
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
答案解析
limit 用法:
select * from tableName limit i,n
# tableName:表名
# i:为查询结果的索引值(默认从0开始),当i=0时可省略i
# n:为查询结果返回的数量
# i与n之间使用英文逗号","隔开
关键字 distinct 去重
SELECT DISTINCT name from A
查询第二大, 先查询全部,然后去重,然后降序排序,然后获取limit获取第二个
如果上面的操作没有查到值,那就返回null, 然后给查询内容命名
SELECT IFNULL(
(SELECT DISTINCT salary
FROM employee
ORDER BY salary DESC
LIMIT 1, 1), null) SecondHighestSalary
总结:
- MySQL没有全外连接,用左右连接 + union all
- 把整行的拆散为多行用union, 把多行合并成整行用 group by
- 如果not in 集合里有null值,那直接返回null 对应结果为false,不会真正判断有没有在集合里
- case 语句用法 CASE WHEN condition THEN result1 ELSE result2 END
- DISTINCT去重 SELECT DISTINCT name from A
- LIMIT 用法 取i - n,从0开始: SELECT * FROM table limit i,n
边栏推荐
- B_QuRT_User_Guide(30)
- SQL analysis (query interception analysis for SQL optimization)
- Sword finger offer 03 Duplicate number in array
- 解决npm ERR! Unexpected end of JSON input while parsing near问题
- NLP sequence can completely simulate human brain intelligence
- Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu
- Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week
- 新唐NUC980使用记录:自制开发板(基于NUC980DK61YC)
- Trigonometric transformation formula
- Installing mysql5.7 under Windows
猜你喜欢
![[JS] - [DFS, BFS application] - learning notes](/img/77/6f8d4ebe1d0b3ba036aea9358de793.png)
[JS] - [DFS, BFS application] - learning notes

The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion

匿名页的反向映射

B_ QuRT_ User_ Guide(27)

Set the encoding of CMD to UTF-8

nlp序列完全可以模拟人脑智能

The maximum number of Rac open file descriptors, and the processing of hard check failure

Activity implicit jump

Why MySQL cannot insert Chinese data in CMD

Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week
随机推荐
PC端隐藏滚动条
Set the encoding of CMD to UTF-8
B_QuRT_User_Guide(27)
Trigonometric transformation formula
城联优品向英德捐赠抗洪救灾爱心物资
B_ QuRT_ User_ Guide(27)
Redis persistence problem and final solution
图像翻译/Transformer:ITTR: Unpaired Image-to-Image Translation with Transformers用Transfor进行非配对图像对图像的转换
NPM clean cache
Introduction, compilation, installation and deployment of Doris learning notes
The micro kernel zephyr is supported by many manufacturers!
Selenium+chromedriver cannot open Google browser page
Connaissez - vous le protocole TCP (2)?
Unity - use of API related to Pico development input system ---c
Tree
设置cmd的编码为utf-8
NLP sequence can completely simulate human brain intelligence
【学习笔记】线性基
App automated testing appium Tutorial Part 1 - advanced supplementary content
Set<String>