【SQL】1327. 列出指定时间段内所有的下单产品

前述

知识点:sql中用JOIN USING 简化JOIN ON

题目描述

leetcode 题目:1327. 列出指定时间段内所有的下单产品



Code

写法一

先过滤,再连表

sql 复制代码
-- 写法一:先过滤再连表
select B.product_name, A.summ as unit
from(
    select product_id,
        sum(unit) as summ
    from Orders
    where order_date between '2020-02-01' and '2020-02-29'
    group by product_id
    having summ >= 100
) A
left join Products B
on A.product_id = B.product_id;

写法二

先连表,再过滤

sql 复制代码
-- 写法二:先连表再过滤
select product_name, sum(unit) as unit
from Products P
left join Orders O
using(product_id)
-- where order_date between '2020-02-01' and '2020-02-29'
-- where order_date >= '2020-02-01' and order_date <='2020-02-29'
-- where year(order_date) = 2020 and month(order_date) = 02
where order_date like '2020-02%'
group by O.product_id
having sum(unit) >= 100;

不同的过滤条件写法,都行。

相关推荐
夜泉_ly2 小时前
MySQL -安装与初识
数据库·mysql
qq_529835353 小时前
对计算机中缓存的理解和使用Redis作为缓存
数据库·redis·缓存
月光水岸New5 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
狄加山6755 小时前
数据库基础1
数据库
我爱松子鱼5 小时前
mysql之规则优化器RBO
数据库·mysql
chengooooooo5 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
Rverdoser6 小时前
【SQL】多表查询案例
数据库·sql
Galeoto6 小时前
how to export a table in sqlite, and import into another
数据库·sqlite
人间打气筒(Ada)7 小时前
MySQL主从架构
服务器·数据库·mysql
leegong231117 小时前
学习PostgreSQL专家认证
数据库·学习·postgresql