当前位置: 首页 > news >正文

力扣SQL刷题5

目录

      • 597. 好友申请 I:总体通过率
      • 602. 好友申请 II :谁有最多的好友
      • 603. 连续空余座位
      • 1045. 买下所有产品的客户

597. 好友申请 I:总体通过率

官方讲的题目太繁琐了,大概就是(表2中列1列2不全相同的行数)/(表1中列1列2不全相同的行数)
题型:如何统计表中列A与列B不全相同的行数
解题:select count(distinct 列A,列B) from 表名
繁琐一点的:select count(*) from (select distinct 列A,列B from 表)

在这里插入图片描述

select round(ifnull((select count(distinct requester_id ,accepter_id) from RequestAccepted) / (select count(distinct sender_id ,send_to_id) from FriendRequest),0),2) as accept_rate ;

select语句中可以没有from

602. 好友申请 II :谁有最多的好友

题型:列A和列B的值一起考虑,分组计数输出次数最多的
解题:(select 列A from 表) union all (select 列B from 表)

在这里插入图片描述

select id,count(*) num
from 
(select requester_id id from RequestAccepted
union all
select accepter_id as id from RequestAccepted) a 
group by id
order by count(1) desc
limit 1

603. 连续空余座位

题型:连续满足条件的行记录才输出,即输出与否与前后行相关
解答:错开一位的自连接

在这里插入图片描述
在这里插入图片描述
解法1:

先和后一号的自连接,该行和下一行都满足条件,则输出该行
union
和前一号的自连接,该行和上一行都满足条件,则输出该行

select *
from
(
(select c1.seat_id seat_id
from Cinema c1 left join Cinema c2 on c1.seat_id = c2.seat_id-1
where c1.free = 1 and c2.free = 1)
union 
(select c1.seat_id seat_id
from Cinema c1 left join Cinema c2 on c1.seat_id = c2.seat_id+1
where c1.free = 1 and c2.free = 1)
) a
order by seat_id

解法2:解法1太繁琐,用abs(c1.seat_id - c2.seat_id) = 1把解法1中两种情况结合起来

select distinct c1.seat_id seat_id
from Cinema c1 left join Cinema c2 on abs(c1.seat_id - c2.seat_id) = 1
where c1.free = 1 and c2.free = 1
order by seat_id

1045. 买下所有产品的客户

题型:表1中,对列A分组,如果组中列B的值涵盖了所有表2的值,则输出对应的列A类别–见题清楚些
解法:利用外键的限制,不会有超出表2的情况,所以只要对比数量满足,就一定全部涵盖了

在这里插入图片描述

在这里插入图片描述
group by分组后,看各类别的计数和表2的是否相等

select customer_id
from Customer
group by customer_id
having count(distinct product_key) = (select count(product_key) from Product)
http://www.lryc.cn/news/75.html

相关文章:

  • 动态规划详解(完结篇)——如何抽象出动态规划算法?以及解题思路
  • C语言一维数组篇【下】——每日刷题经验分享
  • VHDL语言基础-组合逻辑电路-其它组合逻辑模块
  • 初识Vue
  • TOUGH系列软件建模实践方法及在地下水、CO2地质封存、水文地球化学、地热等多相多组分系统多过程耦合
  • Codeforces Round #699 (Div. 2)
  • MySQL存储过程的传参和流程控制
  • MySQl学习(从入门到精通11)
  • 关于ThreadLocal
  • 【C++】类和对象(中)
  • js下载文件
  • ESP8266 + STC15+ I2C OLED带网络校时功能的定时器时钟
  • 计算机入门基础知识大全
  • Python程序出现错误怎么办?
  • 【Vue3】v-if和v-for优先级
  • Windows上实现 IOS 自动化测试
  • Linux云服务器下怎么重置MySQL8.0数据库密码
  • JVM调优
  • 【配电网规划】SOCPR和基于线性离散最优潮流(OPF)模型的配电网规划( DNP )(Matlab代码实现)
  • 锦正茂EM3电磁铁的技术参数
  • Go最新版下载 Go1.20版新特性
  • Pywirt:一款基于Python的Windows安全应急响应工具
  • KDZD832 智能蓄电池活化仪
  • 纯css实现loading加载中(多种展现形式)
  • 【面试题】2023 vue高频面试知识点汇总
  • 跨境电商选品重要吗?
  • SpringBoot
  • python--turtle
  • NodeJS的后端Express项目部署到Ubuntu服务器,为前端提供API服务
  • 作为研发如何使用Github Api?