【力扣 | SQL题 | 每日三题】力扣1264, 1113, 1098, 1082

news/2024/10/4 15:13:52/

1. 力扣1264:页面推荐

1.1 题目:

朋友关系列表: Friendship

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user1_id      | int     |
| user2_id      | int     |
+---------------+---------+
(user1_id, user2_id) 是这张表具有唯一值的列的组合。
这张表的每一行代表着 user1_id 和 user2_id 之间存在着朋友关系。

喜欢列表: Likes

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| user_id     | int     |
| page_id     | int     |
+-------------+---------+
(user_id, page_id) 是这张表具有唯一值的列的组合。
这张表的每一行代表着 user_id 喜欢 page_id。

编写解决方案,向user_id = 1 的用户,推荐其朋友们喜欢的页面。不要推荐该用户已经喜欢的页面。

以 任意顺序 返回结果,其中不应当包含重复项。

返回结果的格式如下例所示。

示例 1:

输入:
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1        | 2        |
| 1        | 3        |
| 1        | 4        |
| 2        | 3        |
| 2        | 4        |
| 2        | 5        |
| 6        | 1        |
+----------+----------+Likes table:
+---------+---------+
| user_id | page_id |
+---------+---------+
| 1       | 88      |
| 2       | 23      |
| 3       | 24      |
| 4       | 56      |
| 5       | 11      |
| 6       | 33      |
| 2       | 77      |
| 3       | 77      |
| 6       | 88      |
+---------+---------+输出:
+------------------+
| recommended_page |
+------------------+
| 23               |
| 24               |
| 56               |
| 33               |
| 77               |
+------------------+
解释:
用户1 同 用户2, 3, 4, 6 是朋友关系。
推荐页面为: 页面23 来自于 用户2, 页面24 来自于 用户3, 页面56 来自于 用户3 以及 页面33 来自于 用户6。
页面77 同时被 用户2 和 用户3 推荐。
页面88 没有被推荐,因为 用户1 已经喜欢了它。

1.2 思路:

看注释。

1.3 题解:

sql">--因为用户1可能喜欢多个页面,所以将其全部过滤掉
-- where page_id not in (
--         select page_id
--         from Likes
--         where user_id = 1
-- )--if函数收集用户1的所有朋友
-- where user_id in (
--     select if(user1_id = 1, user2_id, user1_id) user_id 
--     from Friendship
--     where user1_id = 1 or user2_id = 1
-- )--从过滤的l表中逐一比较记录,判断是否是朋友喜欢的页面-- 最后不要忘了过滤一下重复元素select distinct page_id recommended_page
from (select *from Likes where page_id not in (select page_idfrom Likeswhere user_id = 1)) l 
where user_id in (select if(user1_id = 1, user2_id, user1_id) user_id from Friendshipwhere user1_id = 1 or user2_id = 1
)

2. 力扣1113:报告的记录

2.1 题目:

动作表:Actions

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user_id       | int     |
| post_id       | int     |
| action_date   | date    | 
| action        | enum    |
| extra         | varchar |
+---------------+---------+
此表可能会有重复的行。
action 字段是 ENUM 类型的,包含:('view', 'like', 'reaction', 'comment', 'report', 'share')
extra 包含关于 action 的可选信息,例如举报的原因或反馈的类型。
当 action 为 'report' 时 extra 不会为 NULL。

编写解决方案,针对每个举报原因统计昨天的举报帖子数量。假设今天是 2019-07-05 。

返回结果表 无顺序要求 

结果格式如下示例所示。

示例 1:

输入:
Actions table:
+---------+---------+-------------+--------+--------+
| user_id | post_id | action_date | action | extra  |
+---------+---------+-------------+--------+--------+
| 1       | 1       | 2019-07-01  | view   | null   |
| 1       | 1       | 2019-07-01  | like   | null   |
| 1       | 1       | 2019-07-01  | share  | null   |
| 2       | 4       | 2019-07-04  | view   | null   |
| 2       | 4       | 2019-07-04  | report | spam   |
| 3       | 4       | 2019-07-04  | view   | null   |
| 3       | 4       | 2019-07-04  | report | spam   |
| 4       | 3       | 2019-07-02  | view   | null   |
| 4       | 3       | 2019-07-02  | report | spam   |
| 5       | 2       | 2019-07-04  | view   | null   |
| 5       | 2       | 2019-07-04  | report | racism |
| 5       | 5       | 2019-07-04  | view   | null   |
| 5       | 5       | 2019-07-04  | report | racism |
+---------+---------+-------------+--------+--------+
输出:
+---------------+--------------+
| report_reason | report_count |
+---------------+--------------+
| spam          | 1            |
| racism        | 2            |
+---------------+--------------+ 
解释:注意,我们只关心举报帖数量非零的举报原因。

2.2 思路:

我也没看懂题目的意思,根据输入输出表凑的答案。

2.3 题解:

sql">select extra report_reason, count(distinct post_id) report_count
from (select *from Actionswhere action_date = '2019-07-04' and extra is not nulland action = 'report'
) a 
group by extra

3. 力扣1098:小众书籍

3.1 题目:

书籍表 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" |
+-----------+--------------------+

3.2 思路:

看注释。

3.3 题解:

sql">select b.book_id, name
from (
-- 不考虑 上架距今销售 不满一个月 的书籍 select book_id, namefrom Bookswhere available_from <= '2019-05-23'
) b left join (select book_id, sum(quantity) sum, count(*) cntfrom Orders o -- 过去一年中订单总量 少于 10 本 的 书籍where dispatch_date >= '2018-06-23' and dispatch_date <= '2019-06-23'group by book_id
) o
on o.book_id = b.book_id
-- -- 左外连接,如果cnt为null表明该书在过去一年订单量为0,满足题目意思。
where cnt is null or sum < 10

4. 力扣1082:销售分析1

4.1 题目:

产品表:Product

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| product_id   | int     |
| product_name | varchar |
| unit_price   | int     |
+--------------+---------+
product_id 是这个表的主键(具有唯一值的列)。
该表的每一行显示每个产品的名称和价格。

销售表:Sales

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| seller_id   | int     |
| product_id  | int     |
| buyer_id    | int     |
| sale_date   | date    |
| quantity    | int     |
| price       | int     |
+------ ------+---------+
这个表它可以有重复的行。 
product_id 是 Product 表的外键(reference 列)。
该表的每一行包含关于一个销售的一些信息。

编写解决方案,找出总销售额最高的销售者,如果有并列的,就都展示出来。

以 任意顺序 返回结果表。

返回结果格式如下所示。

示例 1:

输入:
Product 表:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1          | S8           | 1000       |
| 2          | G4           | 800        |
| 3          | iPhone       | 1400       |
+------------+--------------+------------+
Sales 表:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date  | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1         | 1          | 1        | 2019-01-21 | 2        | 2000  |
| 1         | 2          | 2        | 2019-02-17 | 1        | 800   |
| 2         | 2          | 3        | 2019-06-02 | 1        | 800   |
| 3         | 3          | 4        | 2019-05-13 | 2        | 2800  |
+-----------+------------+----------+------------+----------+-------+
输出:
+-------------+
| seller_id   |
+-------------+
| 1           |
| 3           |
+-------------+
解释:Id 为 1 和 3 的销售者,销售总金额都为最高的 2800。

4.2 思路:

看注释。

4.3 题解:

sql">-- 先找到每个卖者的销售者总和
-- from (
--     select sum(price) sum
--     from Sales
--     group by seller_id
-- ) s-- 然后再找总销售额最大的价钱
-- select max(sum)
--     from (
--         select sum(price) sum
--         from Sales
--         group by seller_id
--     ) sselect seller_id
from Product p
join Sales s 
on p.product_id = s.product_id   
group by seller_id
having sum(price) = (select max(sum)from (select sum(price) sumfrom Salesgroup by seller_id) s
)


http://www.ppmy.cn/news/1534479.html

相关文章

vue访问组件的数据和方法

组件源码 <template><view class"c_container" :style"myStyle" click"clickCust"><view style"font-size: 18px;text-align: center;">{{item.name}}</view><view style"margin-top: 10px;font-siz…

C++模拟实现vector容器【万字模拟✨】

更多精彩内容..... &#x1f389;❤️播主の主页✨&#x1f618; Stark、-CSDN博客 本文所在专栏&#xff1a; 学习专栏C语言_Stark、的博客-CSDN博客 项目实战C系列_Stark、的博客-CSDN博客 数据结构与算法_Stark、的博客-CSDN博客 座右铭&#xff1a;梦想是一盏明灯&#xff…

lambda表达式底层实现:反编译LambdaMetafactory + 转储dump + 运行过程 + 反汇编 + 动态指令invokedynamic

一、结论先行 lambda 底层实现机制 1.lambda 表达式的本质&#xff1a;函数式接口的匿名子类的匿名对象 2.lambda表达式是语法糖 语法糖&#xff1a;编码时是lambda简洁的表达式&#xff0c;在字节码期&#xff0c;语法糖会被转换为实际复杂的实现方式&#xff0c;含义不变&am…

Arduino UNO R3自学笔记17 之 Arduino为啥要用中断?

注意&#xff1a;学习和写作过程中&#xff0c;部分资料搜集于互联网&#xff0c;如有侵权请联系删除。 前言&#xff1a;学习Arduino中断的概念及其功能。 1.什么是中断&#xff1f; 单片机在执行程序时&#xff0c;发生一些其它紧急的事情&#xff0c;单片机将立即暂停当前…

Python编码系列—Python访问者模式:为对象结构添加新功能的艺术

&#x1f31f;&#x1f31f; 欢迎来到我的技术小筑&#xff0c;一个专为技术探索者打造的交流空间。在这里&#xff0c;我们不仅分享代码的智慧&#xff0c;还探讨技术的深度与广度。无论您是资深开发者还是技术新手&#xff0c;这里都有一片属于您的天空。让我们在知识的海洋中…

工作笔记20240927——vscode + jlink调试

launch.json的配置&#xff0c;可以用的 {"name": "Debug","type": "cppdbg","request": "launch","miDebuggerPath": "./arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-g…

看480p、720p、1080p、2k、4k、视频一般需要多大带宽呢?

看视频都喜欢看高清&#xff0c;那么一般来说看电影不卡顿需要多大带宽呢&#xff1f; 以4K为例&#xff0c;这里引用一位网友的回答&#xff1a;“视频分辨率4092*2160&#xff0c;每个像素用红蓝绿三个256色(8bit)的数据表示&#xff0c;视频帧数为60fps&#xff0c;那么一秒…

Spring:强制登陆与拦截器

1.只使用session验证 &#xff08;1&#xff09;第一步&#xff1a;用户登陆时存储session ApiOperation("用户登陆") PostMapping("/login") public AppResult login(HttpServletRequest request,RequestParam("username") ApiParam("用…