LeetCode MySQL 1098. 小众书籍
生活随笔
收集整理的這篇文章主要介紹了
LeetCode MySQL 1098. 小众书籍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
書籍表 Books:
+----------------+---------+ | Column Name | Type | +----------------+---------+ | book_id | int | | name | varchar | | available_from | date | +----------------+---------+ book_id 是這個表的主鍵。訂單表 Orders:
+----------------+---------+ | Column Name | Type | +----------------+---------+ | order_id | int | | book_id | int | | quantity | int | | dispatch_date | date | +----------------+---------+ order_id 是這個表的主鍵。 book_id 是 Books 表的外鍵。你需要寫一段 SQL 命令,篩選出過去一年中訂單總量 少于10本 的 書籍 。
注意:不考慮 上架(available from)距今 不滿一個月 的書籍。
并且 假設今天是 2019-06-23 。
下面是樣例輸出結果:
Books 表: +---------+--------------------+----------------+ | book_id | name | available_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 表: +----------+---------+----------+---------------+ | order_id | book_id | quantity | dispatch_date | +----------+---------+----------+---------------+ | 1 | 1 | 2 | 2018-07-26 | | 2 | 1 | 1 | 2018-11-05 | | 3 | 3 | 8 | 2019-06-11 | | 4 | 4 | 6 | 2019-06-05 | | 5 | 4 | 5 | 2019-06-20 | | 6 | 5 | 9 | 2009-02-02 | | 7 | 5 | 8 | 2010-04-13 | +----------+---------+----------+---------------+Result 表: +-----------+--------------------+ | book_id | name | +-----------+--------------------+ | 1 | "Kalila And Demna" | | 2 | "28 Letters" | | 5 | "The Hunger Games" | +-----------+--------------------+來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/unpopular-books
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
2. 解題
# Write your MySQL query statement below select b.book_id, name from Books b left join (select book_id, sum(quantity) sellfrom Orderswhere datediff('2019-06-23', dispatch_date) <= 365group by book_id ) t using(book_id) where available_from <= '2019-05-23'and ifnull(t.sell, 0) < 10我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的LeetCode MySQL 1098. 小众书籍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 727. 最小窗口子序
- 下一篇: LeetCode 1039. 多边形三角