目录
一,原题力扣链接
二,题干
表:
Product
+--------------+---------+ | Column Name | Type | +--------------+---------+ | product_id | int | | product_name | varchar | | unit_price | int | +--------------+---------+ product_id 是该表的主键(具有唯一值的列)。 该表的每一行显示每个产品的名称和价格。表:
Sales
+-------------+---------+ | Column Name | Type | +-------------+---------+ | seller_id | int | | product_id | int | | buyer_id | int | | sale_date | date | | quantity | int | | price | int | +------ ------+---------+ 这个表可能有重复的行。 product_id 是 Product 表的外键(reference 列)。 该表的每一行包含关于一个销售的一些信息。编写解决方案,报告
2019年春季
才售出的产品。即 仅 在2019-01-01
(含)至2019-03-31
(含)之间出售的商品。以 任意顺序 返回结果表。
结果格式如下所示。
示例 1:
输入: Product table: +------------+--------------+------------+ | product_id | product_name | unit_price | +------------+--------------+------------+ | 1 | S8 | 1000 | | 2 | G4 | 800 | | 3 | iPhone | 1400 | +------------+--------------+------------+Sales
table: +-----------+------------+----------+------------+----------+-------+ | seller_id | product_id | buyer_id | sale_date | quantity | price | +-----------+------------+----------+------------+----------+-------+ | 1 | 1 | 1 | 2019-01-21 | 2 | 2000 | | 1 | 2 | 2 | 2019-02-17 | 1 | 800 | | 2 | 2 | 3 | 2019-06-02 | 1 | 800 | | 3 | 3 | 4 | 2019-05-13 | 2 | 2800 | +-----------+------------+----------+------------+----------+-------+ 输出: +-------------+--------------+ | product_id | product_name | +-------------+--------------+ | 1 | S8 | +-------------+--------------+ 解释: id 为 1 的产品仅在 2019 年春季销售。 id 为 2 的产品在 2019 年春季销售,但也在 2019 年春季之后销售。 id 为 3 的产品在 2019 年春季之后销售。 我们只返回 id 为 1 的产品,因为它是 2019 年春季才销售的产品。
三,建表语句
Create table If Not Exists Product (product_id int, product_name varchar(10), unit_price int)
Create table If Not Exists Sales (seller_id int, product_id int, buyer_id int, sale_date date, quantity int, price int)
Truncate table Product;
insert into Product (product_id, product_name, unit_price) values ('1', 'S8', '1000')
insert into Product (product_id, product_name, unit_price) values ('2', 'G4', '800')
insert into Product (product_id, product_name, unit_price) values ('3', 'iPhone', '1400');
Truncate table Sales;
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('1', '1', '1', '2019-01-21', '2', '2000')
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('1', '2', '2', '2019-02-17', '1', '800')
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('2', '2', '3', '2019-06-02', '1', '800')
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('3', '3', '4', '2019-05-13', '2', '2800');
四,分析
第一个表的结构 产品表 有产品id 有产品名称 有产品单价 第二个表的结构 有卖家id 有产品id 有买家id 有交易时间 有交易数量 有交易价格 题目要求 2019年从春季 春季 即2019-01-01到2019-3-31 之间的商品id和名称 简单查询 拼接2个表 拿到商品id和商品名称即可 条件是春季 方案一 写死的春季天数 方案二 季度函数 坑点: 仅再春节销售的 如果不是再春季销售的要筛选出来 所以就需要对产品id分组了 以产品id分组 去最小的天数和最大的天数 出来 最小的天数 如果再春季的第一天 最大的天数 如果再春季的最后一天 才算 这样他的就全部都在春季销售了 这个产品
五,SQL解答
解法一,子查询
select product_id,product_name from product where product_id in (
select s.product_id as s_id from product p,sales s
where p.product_id=s.product_id
group by s.product_id having min(sale_date)>='2019-1-1' and max(sale_date)<='2019-3-31'
);
解法二,with上下文 内连接
# 方案二
with t1 as (
select s.product_id as s_id from product p,sales s
where p.product_id=s.product_id
group by s.product_id having min(sale_date)>='2019-1-1' and max(sale_date)<='2019-3-31'
)
select product_id,product_name from product,t1 where product_id=t1.s_id;
六,验证
解法二验证,内连接
解法一验证 子查询
七,知识点总结
总结: 仅仅再春节 需要对用户分组 去最小的天数和最大的天数 然后分别去和春季最后一天和第一天筛选 子查询用in 比= 要好; 子查询改写为with 连接 比较容易阅读 min函数的用法 max函数的用法 内连接的运用