1098. Unpopular Books 难度:中等

1、题目描述

Write an SQL query that reports the books that have sold less than 10 copies in the last year, excluding books that have been available for less than 1 month from today. Assume today is 2019-06-23.

The query result format is in the following example:

Books table:

book_idnameavailable_from
1“Kalila And Demna”2010-01-01
2“28 Letters”2012-05-12
3“The Hobbit”2019-06-10
4“13 Reasons Why”2019-06-01
5“The Hunger Games”2008-09-21

Orders table:

order_idbook_idquantitydispatch_date
1122018-07-26
2112018-11-05
3382019-06-11
4462019-06-05
5452019-06-20
6592009-02-02
7582010-04-13

Result table:

book_idname
1“Kalila And Demna”
2“28 Letters”
5“The Hunger Games”

来源:力扣(LeetCode)

2、解题思路

1# 增加子表,分别计算在2018年卖的数量

select book_id,sum(quantity) as qu
from Orders
where year(dispatch_date)=2018
group by book_id

2# Books表与上表联查,条件是上表卖的数小于10,并且与今天’2019-06-23’相差小于30
where ifnull(qu,0)<=10 and datediff('2019-06-23',available_from)>30

3、提交记录

select b.book_id,b.name
from Books b left join 

(select book_id,sum(quantity) as qu
from Orders
where year(dispatch_date)=2018
group by book_id)jude

on b.book_id=jude.book_id
where ifnull(qu,0)<=10 and datediff('2019-06-23',available_from)>30

系统出错,无法通过

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值