news 2026/4/16 6:04:12

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/4/15 21:09:53

Python 3 文件操作详解

Python 3 文件操作详解 引言 Python 3 作为一种强大的编程语言,在文件操作方面提供了丰富的功能。无论是读取、写入还是修改文件,Python 都能轻松应对。本文将详细介绍 Python 3 中文件操作的相关知识,包括文件打开、读取、写入、关闭等基本操作,以及文件路径处理、文件权…

作者头像 李华
网站建设 2026/4/4 15:14:50

微爱帮监狱写信寄信小程序与中国邮政焦作支局战略合作技术方案

一、合作背景微爱帮与中国邮政焦作支局达成战略合作&#xff0c;共同构建安全、可靠的监狱信件传输体系&#xff0c;确保特殊群体通信的合法合规与信息安全。二、信件安全保障方案1. 双层安全审核机制class LetterSecuritySystem:def __init__(self):self.content_scanner Con…

作者头像 李华
网站建设 2026/4/15 22:09:54

如何轻松的写一篇内容

管不住顺手刷社媒&#xff0c;那就反手写内容&#xff01;2024年做个决定&#xff0c;每周关注一个话题&#xff0c;然后写一篇内容&#xff0c;要尽量用花里胡哨的表达风格。就是为了练键盘&#xff0c;文字随便敲。如果只写专业的方向&#xff0c;固定的实践经验和思路&#…

作者头像 李华
网站建设 2026/4/10 20:33:59

GCC -fhardened 面向生产构建的安全加固落地指南

GCC 14 引入的 -fhardened 是一个面向生产环境的安全加固选项集合&#xff0c;旨在不改变 ABI 的前提下&#xff0c;系统性启用主流 Linux 平台的编译期与链接期防护能力。本文将深入解析 -fhardened 的设计目标、与 PIE/ASLR 的关系、与 LTO 的已知问题&#xff0c;以及在 CMa…

作者头像 李华
网站建设 2026/4/15 13:50:54

首尔大学团队揭秘:为什么AI绘画总是用“高斯分布“?

这项由首尔大学数据科学研究院的李俊豪、金官锡和李俊锡团队完成的研究发表于2025年12月的《机器学习研究汇刊》&#xff08;Transactions on Machine Learning Research&#xff09;&#xff0c;感兴趣的读者可以通过论文编号arXiv:2512.18184查阅完整内容。说到AI绘画&#x…

作者头像 李华