LeetCode 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) 。如果这一产品在商店里没有出售,则不输出这一行。
输出结果表中的 顺序不作要求 。
查询输出格式请参考下面示例。
示例 1:
输入:
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 |
±-----------±-------±------+
解释:
产品0在store1,store2,store3的价格分别为95,100,105。
产品1在store1,store3的价格分别为70,80。在store2无法买到。
# Write your MySQL query statement below
SELECT product_id, 'store1' store, store1 price
FROM Products
WHERE store1 IS NOT NULL
UNION
SELECT product_id, 'store2' store, store2 price
FROM Products
WHERE store2 IS NOT NULL
UNION
SELECT product_id, 'store3' store, store3 price
FROM Products
WHERE store3 IS NOT NULL;
以上SQL中,判断某列是否为NULL要用IS NOT NULL,如果用WHERE store1 != NULL,会永远返回0行。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2020-02-18 剑指offer 学习笔记 位运算
2019-02-18 JAVA数据类型