当前位置:网站首页>【力扣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
边栏推荐
- Three step problem of leetcode
- The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion
- Selenium reptile
- Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19
- Anniversary party
- A - 深海探险
- Trigonometric transformation formula
- B_ QuRT_ User_ Guide(28)
- [JS] - [throttling and anti shake function]
- MySQL single table access method
猜你喜欢

第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开

Jacobian matrix J commonly used in slam

Prometheus deployment alarm docking QQ mailbox

块级元素上下左右居中的两个小技巧

B_ QuRT_ User_ Guide(27)

Eslint 语法监测关闭

B_QuRT_User_Guide(26)

Reverse mapping of anonymous pages

NLP sequence can completely simulate human brain intelligence

MySQL two table connection principle (understand join buf)
随机推荐
Is it reliable for the top ten securities companies to register and open accounts? Is it safe?
Installing mysql5.7 under Windows
安装nrm后,使用nrm命令报错internal/validators.js:124 throw new ERR_INVALID_ARG_TYPE(name, ‘string‘, value)
RAC enable archive log
PLSQL installation under Windows
App automated testing appium tutorial 2 - ADB command
Connaissez - vous le protocole TCP (2)?
探讨gis三维系统在矿山行业中的应用
图像翻译/Transformer:ITTR: Unpaired Image-to-Image Translation with Transformers用Transfor进行非配对图像对图像的转换
A - 深海探险
Three step problem of leetcode
Resolution of Rac grid failure to start after server restart
Jenkins' common build trigger and hook services (V)
Redis cerebral fissure
解决npm ERR! Unexpected end of JSON input while parsing near问题
B_ QuRT_ User_ Guide(30)
【学习笔记】搜索
SQL master-slave replication setup
微内核Zephyr获众多厂家支持!
How to use redis to solve concurrency problems