news 2026/5/1 9:19:25

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/5/1 8:55:02

NumPy 字符串函数

NumPy 字符串函数 引言 NumPy 是 Python 中一个强大的科学计算库,它提供了大量的函数和方法来处理数值数据。除了数值计算外,NumPy 也支持字符串操作。本文将详细介绍 NumPy 中的字符串函数,帮助您更好地理解和应用这些函数。 NumPy 字符串函数概述 NumPy 提供了一系列用…

作者头像 李华
网站建设 2026/5/1 7:22:40

光影向善,大爱无界!第44届华鼎奖好莱坞揭晓 安吉丽娜·朱莉榜上有名

时值世界电影诞辰130周年的重要节点&#xff0c;一场汇聚星光与大爱的公益盛会于好莱坞璀璨启幕——第44届全球公益和慈善华鼎奖以发布会形式隆重揭晓获奖名单。作为华鼎奖品牌升级后首次推出的公益专项评选&#xff0c;本届盛典以“影视赋能公益”为核心&#xff0c;打破传统奖…

作者头像 李华
网站建设 2026/5/1 8:47:43

滚珠丝杆直线导轨厂家哪家适配自动化设备高精度传动需求?

自动化设备的精度越来越卷——从3C产品的微小元件装配到半导体的晶圆传输&#xff0c;都需要微米级的定位精度。很多用户问我&#xff1a;“哪些滚珠丝杆直线导轨厂家能适配这种高精度需求&#xff1f;”今天就来聊聊这个话题&#xff0c;结合实际场景&#xff0c;看看什么样的…

作者头像 李华
网站建设 2026/5/1 6:10:20

Spring AI Alibaba — ChatModel与ChatClient的联系与区别

下面是针对 Spring AI Alibaba&#xff08;基于 Spring AI&#xff09;中 ChatModel 和 ChatClient 的联系与区别&#xff0c;以及 链式代码&#xff08;Fluent API&#xff09;与样板代码&#xff08;Boilerplate Code&#xff09;区别的说明&#xff0c;并附带 Java 示例与完…

作者头像 李华
网站建设 2026/5/1 8:00:40

cesium 根据经纬度高度进行额度补偿

const offsetvalue 90; /***度数补偿值*/ const setCameraPosition async (lat: number, lon: number, du: number) > {var point turf.point([lat, lon]);var distance 600 * 1.732;var bearing du - 180 offsetvalue;var options: any { units: "kilometers&q…

作者头像 李华