数据导入:
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');
销售表 Sales
:
+-------------------+--------+
| Column Name | Type |
+-------------------+--------+
| sale_id | int |
| product_id | int |
| year | int |
| quantity | int |
| price | int |
+-------------------+--------+
(sale_id, year) 是销售表 Sales 的主键.
product_id 是关联到产品表 Product 的外键.
注意: price 表示每单位价格
产品表 Product
:
+--------------------+----------+
| Column Name | Type |
+--------------------+----------+
| product_id | int |
| product_name | varchar |
+-------------------+-----------+
product_id 是表的主键.
写一条SQL 查询语句获取 Sales 表中所有产品对应的 产品名称 product_name 以及该产品的所有 售卖年份 year 和 价格 price 。
查询结果中的顺序无特定要求。
查询结果格式示例如下:
Sales 表:
+----------+--------------+-------+-----------+--------+
| sale_id | product_id | year | quantity | price |
+----------+--------------+-------+-----------+--------+
| 1 | 100 | 2008 | 10 | 5000 |
| 2 | 100 | 2009 | 12 | 5000 |
| 7 | 200 | 2011 | 15 | 9000 |
+----------+--------------+--------+----------+--------+Product 表:
+---------------+-------------------+
| product_id | product_name |
+---------------+-------------------+
| 100 | Nokia |
| 200 | Apple |
| 300 | Samsung |
+---------------+-------------------+Result 表:
+-------------------+--------+-------+
| product_name | year | price |
+-------------------+--------+-------+
| Nokia | 2008 | 5000 |
| Nokia | 2009 | 5000 |
| Apple | 2011 | 9000 |
+-------------------+--------+-------+
思路
本题考察表格连接的基本使用,从而获取 Sales 表中所有产品对应的 产品名称product_name 以及该产品的所有 售卖年份 year 和 价格 price 。
算法
使用left join或者join都可以,因为这里的case是Sales表中的Sale_id是包含于Product表中的product_id的,所以不会出现null的情况。
答案
select
p.product_name,
s.year,
s.price
from Sales s
left join Product p
on s.product_id=p.product_id;
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/product-sales-analysis-i
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。