码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • [1795]. 每个产品在不同商店的价格


    文章目录

    • 1.题目
    • 2.示例:
    • 3.答案

    1.题目

    表: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) 。如果这一产品在商店里没有出售,则不输出这一行。输出结果表中的 顺序不作要求 。

    2.示例:

    输入:
    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无法买到。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.答案

    注意: 价格为null的信息不用输出
    这里只要三个商店,可以查询每个产品每个商店的数据信息拼接起来。

    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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    将结果表转为原始表

    注意这里用到了group by分组,select后面只能跟集函数或者分组属性。
    所以 if(store=‘store1’,price,null) store1需要加上集函数,
    sum(if(store=‘store1’,price,null)) store1 或者
    max(if(store=‘store1’,price,null)) store1 或者
    min(if(store=‘store1’,price,null)) store1都可以

    SELECT 
      product_id,
     MAX(IF(store = 'store1', price, NULL)) 'store1',
     SUM(IF(store = 'store2', price, NULL)) 'store2',
     MIN(IF(store = 'store3', price, NULL)) 'store3' 
    FROM
      Products1
    GROUP BY product_id ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/rearrange-products-table

  • 相关阅读:
    LeetCode 332. Reconstruct Itinerary【欧拉回路,通路,DFS】困难
    案例分享——低压电力线载波通信模组(借助电源线实现远距离数据传输、宽压输入、波特率范围广、应用场景多样化)
    【通义千问】Qwen从本地加载分词器报错‘‘tokenizer class not exist‘‘
    【大数据】计算引擎:Spark核心概念
    第1章 软件架构与需求分析方法
    failed (13: Permission denied) while reading upstream解决方法
    C++面试题精选-2024/06/26
    【知网研学】使用方法
    产业生态重构,数字孪生角色意味着什么?
    Unity --- 文本输入框的使用
  • 原文地址:https://blog.csdn.net/qq_51758329/article/details/127931142
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号