首页 > 数据库 >力扣1127(MySQL)-用户购买平台(困难)

力扣1127(MySQL)-用户购买平台(困难)

时间:2023-04-13 12:00:19浏览次数:31  
标签:1127 力扣 platform 2019 MySQL date desktop spend select

题目:

支出表: Spending

这张表记录了用户在一个在线购物网站的支出历史,该在线购物平台同时拥有桌面端(‘desktop’)和手机端(‘mobile’)的应用程序。
这张表的主键是 (user_id, spend_date, platform)。
平台列 platform 是一种 ENUM ,类型为(‘desktop’, ‘mobile’)。

问题
写一段 SQL 来查找每天 仅 使用手机端用户、仅 使用桌面端用户和 同时 使用桌面端和手机端的用户人数和总支出金额。

查询结果格式如下例所示:

Spending table:

 Result table:

 在 2019-07-01, 用户1 同时 使用桌面端和手机端购买, 用户2 仅 使用了手机端购买,而用户3 仅 使用了桌面端购买。
在 2019-07-02, 用户2 仅 使用了手机端购买, 用户3 仅 使用了桌面端购买,且没有用户 同时 使用桌面端和手机端购买。

建表语句:

1 drop table if EXISTS spending_1127;
2 create table if not exists spending_1127(
3 user_id int,
4 spend_date date,
5 platform enum('desktop','mobile'),
6 amount int
7 );
8 truncate table spending_1127;
9 insert into spending_1127 values(1, '2019-07-01', 'mobile','100'),(1, '2019-07-01', 'desktop','100'),(2, '2019-07-01', 'mobile','100'),(2, '2019-07-02', 'mobile','100'),(3, '2019-07-01', 'desktop','100'),(3, '2019-07-02', 'desktop','100');

解题思路:

这道题对于我来说有点困难,参考了一下其他博主的题解

①先查询出每个用户id的platform情况以及交易总额;

1 select user_id, spend_date,if(count(distinct platform) = 2, 'both', platform) as platform,sum(amount) as total_amount
2 from spending_1127
3 group by user_id, spend_date;

 ②再创建一个临时表,列出platform的情况;

1 select 'desktop' as platform union
2 select 'mobile' as platform union
3 select 'both' as platform 

 ③再使上面两步查询出的临时表内连接,以spend_date,platform分组,使用case when进行数量统计;

 1 select spend_date,b.platform,
 2 sum(case when a.platform = b.platform then total_amount else 0 end) as total_amount,
 3 count(if(a.platform = b.platform, 1, null)) as total_users
 4 from (
 5     select user_id, spend_date,if(count(distinct platform) = 2, 'both', platform) as platform,sum(amount) as total_amount
 6     from spending_1127
 7     group by user_id, spend_date
 8 ) as a, (
 9     select 'desktop' as platform union
10     select 'mobile' as platform union
11     select 'both' as platform 
12 )as b
13 group by spend_date,platform
14 order by spend_date

小知识:

内连接分为显示的内连接和隐式的内连接,内连接的结果是笛卡尔积

显示的内连接:有inner join关键字,有on关键字表示的条件;

隐式的内连接:用逗号分隔两个数据库表,条件的表示只能用where。

标签:1127,力扣,platform,2019,MySQL,date,desktop,spend,select
From: https://www.cnblogs.com/liu-myu/p/17314177.html

相关文章

  • 力扣1126(MySQL)-查询活跃业务(中等)
    题目:事件表:Events此表的主键是(business_id,event_type)。表中的每一行记录了某种类型的事件在某些业务中多次发生的信息。问题写一段SQL来查询所有活跃的业务。如果一个业务的某个事件类型的发生次数大于此事件类型在所有业务中的平均发生次数,并且该业务至少有两个这样......
  • Mysql
    CREATETABLEYH(idintPRIMARYKEYCOMMENT'主键id',namevarchar(50)NOTNULLCOMMENT'姓名',ageintCOMMENT'年龄')COMMENT'用户表';创建表SELECTTABLE_COMMENTAS'表注释',TABLE_ROWSAS&......
  • 力扣1132(MySQL)-报告的记录Ⅱ(中等)
    题目:编写一段SQL来查找:在被报告为垃圾广告的帖子中,被移除的帖子的每日平均占比,四舍五入到小数点后2位。Actions表: Removals表:Result表:2019-07-04的垃圾广告移除率是50%,因为有两张帖子被报告为垃圾广告,但只有一个得到移除。2019-07-02的垃圾广告移除率是100%,因......
  • Linux将MySQL数据库目录挂载至新数据盘
    https://www.bbsmax.com/A/QW5YRE1Mdm/具体操作 Mysql如何挂盘?使用linux下的软连接方法,具体操作如下:(以迁移到/home/mysql/目录为例)先停止mysql:/etc/init.d/mysqldstop移动数据:mv/var/lib/mysql/*/home/mysql/创建软连接:ln-s/home/mysql//var/lib/mysql/启动......
  • mysql主从1062主键冲突跳过错误
    1062错误——主键冲突,出现这种情况就是从库出现插入操作,主库又插入相同的数据,iothread没问题,sqlthread出错处理此种错误一般有两种思路:1、直接跳过错误执行语句2、找到错误执行语句,修复主库2数据https://www.cndba.cn/leo1990/article/2957https://www.cndba.cn/leo1990/articl......
  • qrtz表初始化脚本_mysql
    DROPTABLEIFEXISTSqrtz_blob_triggers;DROPTABLEIFEXISTSqrtz_calendars;DROPTABLEIFEXISTSqrtz_cron_triggers;DROPTABLEIFEXISTSqrtz_fired_triggers;DROPTABLEIFEXISTSqrtz_locks;DROPTABLEIFEXISTSqrtz_paused_trigger_grps;DROPTABLEIF......
  • Mysql 入门之过滤数据(二)
    关键字:and、or、in、not知识点1、and并列关系,可对多个列进行过滤2、or或关系,满足条件之一即可3、in指定范围过滤4、not否定关系,取反思考1、and和or若同时存在,哪个优先级最高?2、in使用的优势?3、not存在的意义是什么?案列数据表如下:SELECT*FROMproducts......
  • Mysql四种事务隔离级别
    先了解一下事务的四大特性:ACID原子性(Atomicity)原子性就是不可拆分的特性,要么全部成功然后提交(commit),要么全部失败然后回滚(rollback)。MySQL通过RedoLog重做日志实现了原子性,在将执行SQL语句时,会先写入redologbuffer,再执行SQL语句,若SQL语句执行出错就会根据redologbuffer中的......
  • Mysql 入门之过滤数据(一)
    关键字:where、between、null知识点1、where关键字在Mysql中用于获取指定搜素条件的数据,这些条件也称之为过滤条件2、between为where子句的操作符,诸如=、<、>等符号功能类似3、关于null可以初步理解为它是Mysql中不确定的“值”,从根本上讲它都不是个“值”下面给出一些案......
  • cnetos8上RPM安装mysql8后,修改初始密码提示Your password does not satisfy the curre
    我在修改mysql8初始密码是遇到了Yourpassworddoesnotsatisfythecurrentpolicyrequirements,如果您的mysql版本5.x可能不太合适。下图是我遇到的问题: 意思是,密码不符合密码验证要求。但是mysql8的初始密码连验证要求都查不到,不过一般密码强度是不过我们可以密码设置为......