首页 > 数据库 >leetcode刷题MySQL题解十三

leetcode刷题MySQL题解十三

时间:2022-10-24 21:38:00浏览次数:45  
标签:product store3 store2 store1 题解 id ------- MySQL leetcode


leetcode刷题MySQL题解十三

题目叙述

表: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 as price
from Products
where store1 is not null
union all
select product_id, 'store2' store, store2 as price
from Products
where store2 is not null
union all
select product_id, 'store3' store, store3 as price
from Products
where store3 is not null;

题目运行

leetcode刷题MySQL题解十三_leetcode


标签:product,store3,store2,store1,题解,id,-------,MySQL,leetcode
From: https://blog.51cto.com/u_15844361/5791292

相关文章

  • leetcode刷题MySQL题解十四
    leetcode刷题MySQL题解十四题目叙述给定一个表tree,id是树节点的编号,p_id是它父节点的id。±—±-----+|id|p_id|±—±-----+|1|null||2|1||3|1......
  • leetcode刷题MySQL题解十二
    leetcode刷题MySQL题解十二题目叙述表:Employees±------------±--------+|ColumnName|Type|±------------±--------+|employee_id|int||name|varchar......
  • leetcode刷题MySQL题解十一
    leetcode刷题MySQL题解十一题目叙述表:Weather±--------------±--------+|ColumnName|Type|±--------------±--------+|id|int||recordDate|date||t......
  • leetcode刷题MySQL题解九
    leetcode刷题MySQL题解九题目叙述表Activities:±------------±--------+|列名|类型|±------------±--------+|sell_date|date||product|varchar|±---......
  • CF1753B 题解
    前言题目传送门!更好的阅读体验?其实挺简单的,赛时多打了个等号,被人叉了。思路关键是\(n!\times(n+1)=(n+1)!\)。原因很显然:\((1\times2\times\cdots\tim......
  • MySQL,MVCC详解,快照读在RC、RR下的区别
    MySQL,MVCC详解,快照读在RC、RR下的区别  一、什么是MVCC我们在操作数据库的时候总是这四大类 读读 读写 写读 写写,读读肯定是没有任务数据问题的,但对事物有......
  • mysql索引失效场景
    MySQL索引失效的场景(面试题) 索引失效除了一些常规的,比如使用了某些函数,如:notnull、or等,还有一个是跟数据量有关系的。之前在网上看博客有的博主有提到,当数据量......
  • MySql查询上一篇文章和下一篇文章,上一条数据,下一条数据
      2019-11-2015:50:19    小道仙    107阅读    0评论#blog_article_main表名#bam_id主键id#gmt_create这一行数据创建时间#上一篇文......
  • MySQL执行过程(一条SQL从Java到数据库都经历了什么)
    MySQL执行过程(一条SQL从Java到数据库都经历了什么) 2021-11-1420:44:47    小道仙    100阅读    0评论视频地址 https://www.bilibili.com/video......
  • [abc262E] red and blue gragh 题解
    挺喜欢这道题的,但是因为vp时过了不能写成做题笔记,所以只能写成题解了。绿太配这道题了,实现难度极低,但思维难度较大。AT评了#1719,倒也算恰当。题意:给定一张\(n\)点......