LeetCode刷题-数据库(MySQL)- 512.Game Play Analysis II

该博客介绍了如何使用SQL查询解决LeetCode中的512(Game Play Analysis II)问题。作者首先详细解释了题目的背景和数据表结构,接着分享了解题思路,即找出每个玩家的首次登录设备。最后给出了实现这个查询的SQL代码。

512.Game Play Analysis II

一、题目描述

Table: Activity

Column NameType
player_idint
device_idint
event_datedate
games_playedint

(player_id, event_date) is the primary key of this table.
This table shows the activity of players of some game.
Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device.

Write a SQL query that reports the device that is first logged in for each player.
The query result format is in the following example:
Activity table:

player_iddevice_idevent_dategames_played
122016-03-015
122016-05-026
232017-06-251
312016-03-020
342018-07-035

Result table:

player_iddevice_id
12
23
31

二、思路分析

先找出每个player_id对应的最早登录日期,将其作为一个临时表右连接原表,并通过ON筛选出符合题意的结果。

三、代码实现

SELECT
	a.player_id, a.device_id
FROM
	Activity a
RIGHT JOIN (
		SELECT 
			player_id,
			min(event_date) AS first_date
		FROM
			Activity
		GROUP BY player_id	
	) b
ON
	a.event_date = b.first_date
WHERE
	a.player_id = b.player_id;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值