zoukankan      html  css  js  c++  java
  • [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'
    
  • 相关阅读:
    mybatis系列-04-mybatis开发dao的方法
    mybatis系列-03-入门程序
    mybatis系列-02-mybatis框架
    mybatis系列-01-JDBC
    对代码的理解
    jenkins api调用
    lcov收集覆盖率
    Spring MVC @ModelAttribute
    shell文件/路径处理
    gcc static静态编译选项提示错误修正(/usr/lib/ld: cannot find -lc)
  • 原文地址:https://www.cnblogs.com/coding-gaga/p/13111072.html
Copyright © 2011-2022 走看看