Table:?Activity
+--------------+---------+
| Column Name ?| Type ? ?|
+--------------+---------+
| player_id ? ?| int ? ? |
| device_id ? ?| int ? ? |
| event_date ? | date ? ?|
| games_played | int ? ? |
+--------------+---------+
(player_id, event_date) 是這個表的兩個主鍵
這個表顯示的是某些游戲玩家的游戲活動情況
每一行是在某天使用某個設備登出之前登錄并玩多個游戲(可能為0)的玩家的記錄
請編寫一個 SQL 查詢,描述每一個玩家首次登陸的設備名稱
查詢結果格式在以下示例中:
Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1 ? ? ? ? | 2 ? ? ? ? | 2016-03-01 | 5 ? ? ? ? ? ?|
| 1 ? ? ? ? | 2 ? ? ? ? | 2016-05-02 | 6 ? ? ? ? ? ?|
| 2 ? ? ? ? | 3 ? ? ? ? | 2017-06-25 | 1 ? ? ? ? ? ?|
| 3 ? ? ? ? | 1 ? ? ? ? | 2016-03-02 | 0 ? ? ? ? ? ?|
| 3 ? ? ? ? | 4 ? ? ? ? | 2018-07-03 | 5 ? ? ? ? ? ?|
+-----------+-----------+------------+--------------+
Result table:
+-----------+-----------+
| player_id | device_id |
+-----------+-----------+
| 1 ? ? ? ? | 2 ? ? ? ? |
| 2 ? ? ? ? | 3 ? ? ? ? |
| 3 ? ? ? ? | 1 ? ? ? ? |
+-----------+-----------+
思路:嵌套查詢,對每個玩家,查出最早時間,然后根據最間和玩家id可以選擇出對應的device_id。
select a.player_id as 'player_id',a.device_id as 'device_id'
from activity as a
where a.event_date=(select min(b.event_date) from activity as b where a.player_id=b.player_id);
?