当前位置: 首页 > article >正文

MySQL高阶1777-每家商店的产品价格

题目

找出每种产品在各个商店中的价格。

可以以 任何顺序 输出结果。

准备数据

create database csdn;
use csdn;

Create table If Not Exists Products (product_id int, store ENUM('store1', 'store2', 'store3'), price int);
Truncate table Products;
insert into Products (product_id, store, price) values ('0', 'store1', '95');
insert into Products (product_id, store, price) values ('0', 'store3', '105');
insert into Products (product_id, store, price) values ('0', 'store2', '100');
insert into Products (product_id, store, price) values ('1', 'store1', '70');
insert into Products (product_id, store, price) values ('1', 'store3', '80');

分析数据

第一步:利用if函数将数据拉宽

select
    product_id,
    if(store = 'store1',price,null) as store1,
    if(store = 'store2',price,null) as store2,
    if(store = 'store3',price,null) as store3
from Products;

第二步:最后根据id分组,进行价格统计 

select
    product_id,
    sum(if(store = 'store1',price,null)) as store1,
    sum(if(store = 'store2',price,null)) as store2,
    sum(if(store = 'store3',price,null)) as store3
from Products
group by product_id;

 


http://www.kler.cn/news/310097.html

相关文章:

  • 文心智能体 恐怖类游戏
  • 一.Oracle每日运维操作
  • bug | pycharm社区版无sciview解决办法
  • JVM 调优篇7 调优案例1-堆空间的优化解决
  • Holynix: v1
  • 基于SSM的在线家用电器销售系统
  • 【ARM】Trustzone和安全架构
  • [SDX35+WCN6856]SDX35 + WCN6856 WiFi导致系统crash问题分析及解决方案
  • (娱乐)魔改浏览器-任务栏图标右上角加提示徽章
  • 线性链条件随机场(Linear Chain Conditional Random Field)-有监督学习方法、概率模型、生成模型、对数线性模型、参数化模型
  • 力扣(LeetCode)每日一题 1184. 公交站间的距离
  • 前后端分离Vue美容店会员信息管理系统o7grs
  • Java-使用反射来处理对象,并构建新的JSON数据结构
  • 换个手机IP地址是不是不一样?
  • spring boot admin集成,springboot2.x集成监控
  • .net core8 使用JWT鉴权(附当前源码)
  • Python 之数据库操作(Python Database Operations)
  • Linux(ubuntu)(c语言程序)
  • C++(C++的文件I/O)
  • nanoGPT用红楼梦数据从头训练babyGPT-12.32M实现任意问答
  • Redis 5.0.4 安装教程
  • NFT Insider #147:Sandbox 人物化身九月奖励上线;Catizen 付费用户突破百万
  • PDF转图片的思路思考
  • Leetcode—环形链表||
  • 脚本基本规则
  • C++:日期类的实现
  • java 递归读取前10个匹配的文件所在的全路径
  • 松散绑定是什么?
  • 切换淘宝最新镜像源:优化NPM包管理的极致体验
  • windows C++ 并行编程-异步消息块(一)