news 2026/2/9 8:17:56

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/2/8 23:17:11

Nordic典型芯片nRF5340的功能介绍

目录 概述 1 nRF5340芯片介绍 1.1 芯片特性 1.2 应用领域 1.3 主要规格参数 2 芯片的架构 2.1 MCU模块结构 2.2 片上RAM和Flash空间 2.3 电源管理 2.3.1 System ON mode 2.3.2 功率子模式 2.3.3 System OFF mode 2.3.4 电流消耗列表 2.4 系统时钟 概述 本文介绍…

作者头像 李华
网站建设 2026/2/5 9:10:38

Gin框架基础篇006_HTML模板加载与渲染

在Web开发中&#xff0c;模板渲染是将动态数据嵌入到HTML页面中的关键功能。Gin框架提供了强大且易用的HTML模板渲染功能&#xff0c;基于Go语言内置的html/template包实现。本文将详细介绍Gin框架的HTML模板渲染机制及其使用方法。 1. 模板加载 在使用Gin框架进行HTML模板渲染…

作者头像 李华
网站建设 2026/2/5 15:03:08

nullptr用法

nullptrnullptr是c11用来表示空指针新引入的常量值&#xff0c;在c中如果表示空指针语义时建议使用nullptr而不要使用NULL&#xff0c;因为NULL本质上是个int型的0&#xff0c;其实不是个指针。举例&#xff1a;void func(void *ptr) {cout << "func ptr" <…

作者头像 李华
网站建设 2026/2/7 10:33:23

enum class用法

enum classc11新增有作用域的枚举类型&#xff0c;看代码不带作用域的枚举代码&#xff1a;enum AColor {kRed,kGreen,kBlue };enum BColor {kWhite,kBlack,kYellow };int main() {if (kRed kWhite) {cout << "red white" << endl;}return 0; }如上代码…

作者头像 李华
网站建设 2026/2/7 8:31:23

接口调不通的情况

最近&#xff0c;在做一个项目&#xff0c;对接evolink的api, 发现在他们线上的测试接口是OK的&#xff0c;但是在我本地进行调试时候一直提示 read econnreset一直提示网络连接错误&#xff0c;连接被对方强制重置 通过ping 域名也能ping通&#xff0c; 但就是访问不了别怕&am…

作者头像 李华
网站建设 2026/2/8 4:36:25

APS1604M:办公设备智能升级的“高性能内存引擎”

品牌&#xff1a;AP MEMORY 封装&#xff1a;SOP-8 电压&#xff1a;1.8V免费样品供应渠道-中国区总代理&#xff1a;深圳市贝乐实业股份有限公司在移动打印机与指纹识别仪的智慧进化中&#xff0c;流畅体验与高效处理的核心&#xff0c;往往藏在一块关键芯片里——AP Me…

作者头像 李华