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/8 20:05:14

【mamba-ssm】cuda12.4|python3.12|torch2.6.0保姆级安装手册

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录环境下载安装插播广告环境 linux python3.12.3 torch2.6.0 cuda12.4 使用pip install xxx.whl 方式安装 安装成功的关键是版本对齐 下载 需要下载两个.whl 1、cau…

作者头像 李华
网站建设 2026/6/7 5:43:36

基于SpringBoot的物流管理系统的设计与实现毕业设计项目源码

项目简介本课题以 SpringBoot 框架为核心技术支撑&#xff0c;研发一套适配中小物流企业的全流程物流管理系统&#xff0c;旨在解决传统物流运营中订单处理效率低、货物轨迹不可控、仓储与配送衔接不畅、财务结算繁琐等痛点&#xff0c;实现物流订单、仓储、运输、结算全环节的…

作者头像 李华
网站建设 2026/6/10 11:02:55

PI-36双麦降噪拾音模块:高清拾音,嘈杂环境克星

核心优势&#xff1a;四大亮点&#xff0c;赋能优质体验1. 双核DSP强效降噪&#xff0c;全场景噪音压制内置双核DSP芯片与定制算法&#xff0c;36dB高降噪指标&#xff0c;精准压制稳态与非稳态噪音。配合16KHZ高采样率&#xff0c;清晰萃取人声&#xff0c;大幅提升信噪比&…

作者头像 李华
网站建设 2026/6/6 6:13:30

基本设置模块 Cordova 与 OpenHarmony 混合开发实战

欢迎大家加入开源鸿蒙跨平台开发者社区&#xff0c;一起共建开源鸿蒙跨平台生态。 &#x1f4cc; 概述 基本设置模块为用户提供了配置应用全局行为的入口&#xff0c;包括应用语言、货币单位、默认排序方式、首页展示内容等。模块同时打通了 Web 层配置面板与 OpenHarmony 原生…

作者头像 李华
网站建设 2026/5/28 12:17:41

Gin框架基础篇005_静态文件服务

Web应用通常需要提供静态资源&#xff0c;如CSS、JavaScript、图片等。Gin框架提供了多种方法来处理静态文件。 1. 静态文件服务方法 Gin提供了以下静态文件服务方法&#xff1a; Static() &#xff1a;提供静态文件服务StaticFile()&#xff1a;提供单个静态文件服务StaticFS(…

作者头像 李华
网站建设 2026/5/31 6:43:14

JS逆向 - 某程 w-payload-source 纯算、补环境分析

文章目录概要整体架构流程技术名词解释小结概要 提示&#xff1a;仅供学习&#xff0c;不得用做商业交易&#xff0c;如有侵权请及时联系 也是很久没有更新教学文章了&#xff0c;这次就更新一波纯算和补环境 逆向&#xff1a;某程 header请求头参数 w-payload-source 纯算、补…

作者头像 李华