题解 | #确定哪些订单购买了prod_id为BR01的产#

确定哪些订单购买了 prod_id 为 BR01 的产品(二)

https://www.nowcoder.com/practice/999aa31a9a504c60baa088d90d82e64d

联结、等联结、子查询、子查询+等联结、子查询+联结。太傻逼了

-- 等联结,再加上子查询的方式 就是先查order表中的字段,判断ordernum是否在orderutems条件中
# SELECT
# 	o.cust_id,
# 	o.order_date 
# FROM
# 	OrderItems oi,
# 	Orders o 
# WHERE
# 	oi.order_num = o.order_num 
# 	AND oi.prod_id = 'BR01' 
# ORDER BY
# 	o.order_date 
	
-- 子查询
# SELECT
# 	o.cust_id,
# 	o.order_date 
# FROM
# 	Orders o 
# WHERE
# 	o.order_num IN ( SELECT order_num FROM OrderItems WHERE prod_id = 'BR01' ) 
# ORDER BY
# 	o.order_date 

-- 子查询+等联结(其实这样写很没劲)
# select
# 	o.cust_id,
# 	o.order_date
# from
# 	Orders o,
# 	(select order_num from OrderItems where prod_id = 'BR01') t
# where
# 	o.order_num = t.order_num
# ORDER BY
# 	o.order_date 
-- 联结语法
# SELECT
# 	o.cust_id,
# 	o.order_date 
# FROM
# 	OrderItems oi
# 	INNER JOIN Orders o ON oi.order_num = o.order_num 
# WHERE
# 	oi.prod_id = 'BR01'
# ORDER BY
# 	o.order_date 

-- 子查询+联结(其实这样写很没劲)
SELECT
	o.cust_id,
	o.order_date 
FROM
	Orders o
	INNER JOIN ( SELECT order_num FROM OrderItems WHERE prod_id = 'BR01' ) t ON o.order_num = t.order_num 
ORDER BY
	o.order_date

全部评论

相关推荐

Natrium_:这时间我以为飞机票
点赞 评论 收藏
分享
11-08 13:58
门头沟学院 Java
程序员小白条:竟然是蓝桥杯人才doge,还要花钱申领的offer,这么好的公司哪里去找
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务