news 2026/6/10 12:37:35

letcode数据库题联系

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
letcode数据库题联系

书籍表 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 表的外键(reference 列)。

编写解决方案,筛选出过去一年中订单总量 少于 10 本 的 书籍,并且 不考虑 上架距今销售 不满一个月 的书籍 。假设今天是 2019-06-23 。

返回结果表 无顺序要求 。

结果格式如下所示。

示例 1:

输入:
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 |
±---------±--------±---------±--------------+
输出:
±----------±-------------------+
| book_id | name |
±----------±-------------------+
| 1 | “Kalila And Demna” |
| 2 | “28 Letters” |
| 5 | “The Hunger Games” |
±----------±-------------------+

错误题解析
首先需要明确是查询 book 书籍,books是主表这个是容易忽视的点

错误的题解 select books.book_id,books.name ,Orders.quantity ,Orders.dispatch_date between date_sub('2019-06-23', interval 1 year) and '2019-06-23' from Books left join Orders on Orders.book_id =Books.book_id where #Orders.dispatch_date between date_sub('2019-06-23', interval 1 year) and '2019-06-23' and books.available_from <= date_sub('2019-06-23', interval 1 month) group by books.book_id,books.name #having coalesce(sum(Orders.quantity),0 ) #sum(case when Orders.quantity is null then 0 else Orders.quantity end ) <10 依据 select 语句执行的顺序,先执行 join 语句然后再进行关联的操作,由于条件中返回为null 或者为0会被过滤掉

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/fda02f59b7474b4a8fd5502dc2a4bf79.png) select books.book_id,books.name # ,Orders.quantity from Books left join (select book_id,order_id,quantity from Orders where Orders.dispatch_date between date_sub('2019-06-23', interval 1 year) and '2019-06-23')Orders on Orders.book_id =Books.book_id where books.available_from <= date_sub('2019-06-23', interval 1 month) group by books.book_id,books.name having coalesce(sum(Orders.quantity),0 )<10 #sum(case when Orders.quantity is null then 0 else Orders.quantity end ) <10
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 10:12:18

基于SpringBoot的消防安全知识竞赛系统毕业设计项目源码

项目简介本课题以 SpringBoot 框架为核心技术底座&#xff0c;研发一套面向企事业单位、学校及社区的消防安全知识竞赛系统&#xff0c;旨在解决传统消防安全知识普及形式单一、竞赛组织成本高、答题数据难统计、学习效果难评估等痛点&#xff0c;打造集题库管理、在线竞赛、自…

作者头像 李华
网站建设 2026/6/10 10:12:19

基于SpringBoot的校园传统文化交流系统毕业设计项目源码

项目简介本课题以 SpringBoot 框架为核心技术支撑&#xff0c;研发一套面向高校师生的校园传统文化交流系统&#xff0c;旨在解决校园内传统文化传播形式单一、交流场景分散、资源整合不足、互动参与度低等痛点&#xff0c;打造集文化资源分享、主题交流、活动组织、成果展示于…

作者头像 李华
网站建设 2026/6/10 10:10:56

好用的厦门考研公司

好用的厦门考研公司&#xff1a;福建考研引领高效备考之路在当今竞争激烈的研究生入学考试中&#xff0c;选择一家好用且专业的考研辅导公司至关重要。对于厦门地区的考生而言&#xff0c;[福建考研]凭借其丰富的教学经验和优质的教育资源&#xff0c;成为了众多学子的首选。本…

作者头像 李华
网站建设 2026/6/10 10:09:50

亿可达_自动发邮件攻略

你是否每天重复这样的工作&#xff1a;整理客户邮箱、复制粘贴、手动发送产品邮件&#xff1f;客户越多&#xff0c;工作量越大&#xff0c;还容易出错漏发。现在&#xff0c;亿可达有了更聪明的办法。亿可达自动化流程&#xff0c;完美链接Excel 365和QQ邮箱&#xff0c;实现邮…

作者头像 李华