【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;

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

相关推荐
GDAL25 分钟前
Node.js v22.5+ 官方 SQLite 模块全解析:从入门到实战
数据库·sqlite·node.js
DCTANT1 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Alfred king2 小时前
面试150 生命游戏
leetcode·游戏·面试·数组
AI、少年郎4 小时前
Oracle 进阶语法实战:从多维分析到数据清洗的深度应用(第四课)
数据库·oracle
赤橙红的黄4 小时前
自定义线程池-实现任务0丢失的处理策略
数据库·spring
DataGear4 小时前
如何在DataGear 5.4.1 中快速制作SQL服务端分页的数据表格看板
javascript·数据库·sql·信息可视化·数据分析·echarts·数据可视化
weixin_438335404 小时前
分布式锁实现方式:基于Redis的分布式锁实现(Spring Boot + Redis)
数据库·redis·分布式
码不停蹄的玄黓4 小时前
MySQL Undo Log 深度解析:事务回滚与MVCC的核心功臣
数据库·mysql·undo log·回滚日志
Qdgr_5 小时前
价值实证:数字化转型标杆案例深度解析
大数据·数据库·人工智能
数据狐(DataFox)5 小时前
SQL参数化查询:防注入与计划缓存的双重优势
数据库·sql·缓存