[LeetCode]1084. 销售分析III(Mysql,having+聚合函数)

题目

Table: Product

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| product_id   | int     |
| product_name | varchar |
| unit_price   | int     |
+--------------+---------+
product_id 是这个表的主键
Table: Sales

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| seller_id   | int     |
| product_id  | int     |
| buyer_id    | int     |
| sale_date   | date    |
| quantity    | int     |
| price       | int     |
+------ ------+---------+
这个表没有主键,它可以有重复的行.
product_id 是 Product 表的外键.
 

编写一个SQL查询,报告2019年春季才售出的产品。即仅在2019-01-01至2019-03-31(含)之间出售的商品。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sales-analysis-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

  • 使用having+聚合函数过滤 来代替子查询

代码

# Write your MySQL query statement below
select p.product_id,product_name
from Product p join Sales s
on p.product_id=s.product_id
group by p.product_id
having min(sale_date)>= '2019-01-01' and max(sale_date)<='2019-03-31'

posted on 2020-06-12 23:11  coding_gaga  阅读(222)  评论(0编辑  收藏  举报

导航