力扣高频SQL 50题(基础版)第七题

力扣高频SQL 50题(基础版)第七题

1068. 产品销售分析 I

题目说明

销售表 Sales

±------------±------+

| Column Name | Type |

±------------±------+

| sale_id | int |

| product_id | int |

| year | int |

| quantity | int |

| price | int |

±------------±------+

(sale_id, year) 是销售表 Sales 的主键(具有唯一值的列的组合)。

product_id 是关联到产品表 Product 的外键(reference 列)。

该表的每一行显示 product_id 在某一年的销售情况。

注意: price 表示每单位价格。

产品表 Product

±-------------±--------+

| Column Name | Type |

±-------------±--------+

| product_id | int |

| product_name | varchar |

±-------------±--------+

product_id 是表的主键(具有唯一值的列)。

该表的每一行表示每种产品的产品名称。

编写解决方案,以获取 Sales 表中所有 sale_id 对应的 product_name以及该产品的所有 yearprice

返回结果表 无顺序要求

思路分析

在这里插入图片描述

实现过程

准备数据:
Create table If Not Exists Sales (sale_id int, product_id int, year int, quantity int, price int)
Create table If Not Exists Product (product_id int, product_name varchar(10))
Truncate table Sales
insert into Sales (sale_id, product_id, year, quantity, price) values ('1', '100', '2008', '10', '5000')
insert into Sales (sale_id, product_id, year, quantity, price) values ('2', '100', '2009', '12', '5000')
insert into Sales (sale_id, product_id, year, quantity, price) values ('7', '200', '2011', '15', '9000')
Truncate table Product
insert into Product (product_id, product_name) values ('100', 'Nokia')
insert into Product (product_id, product_name) values ('200', 'Apple')
insert into Product (product_id, product_name) values ('300', 'Samsung')
实现方式:
select p.product_name,s.year,s.price from Sales s,Product p where s.product_id=p.product_id;
结果截图:

在这里插入图片描述

总结:
#隐式内连接语法
select 字段名 from A表名, B表名 where 条件;
  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值