所以我有一个数据库,我使用了select / join语句尝试根据应用用户的游戏数量以及设备上的信息创建排行榜。
我的问题是,如果数据库下载了多个发布版本的应用程序,则该数据库包含一个用户的多行。我的表看起来像:
+--------+---------+----------+-------------+
|# games | user id | platform | app version |
+--------+---------+----------+-------------+
| 15 | 1 | ios | 3.2.1 |
+--------+---------+----------+-------------+
| 13 | 2 | android | 2.0.3 |
+--------+---------+----------+-------------+
| 13 | 2 | android | 3.2.1 |
+--------+---------+----------+-------------+
| 13 | 2 | android | 3.1.0 |
+--------+---------+----------+-------------+
| 11 | 3 | ios | 3.1.5 |
+--------+---------+----------+-------------+
有没有办法将每个唯一用户ID与多行(从使用过多个版本)整合到一行,其中只包含最新版本的信息?也就是说,上表将合并为:
+--------+---------+----------+-------------+
|# games | user id | platform | app version |
+--------+---------+----------+-------------+
| 15 | 1 | ios | 3.2.1 |
+--------+---------+----------+-------------+
| 13 | 2 | android | 3.2.1 |
+--------+---------+----------+-------------+
| 11 | 3 | ios | 3.1.5 |
+--------+---------+----------+-------------+
答案 0 :(得分:0)
如果您想要一行中的所有详细信息,但过滤一列上的最大值(或最小值或某些值),则可以通过多种方式执行此操作。
一种方法是在您加入的派生表中找到每个用户的最大应用版本行以过滤返回的行,另一种方法是使用相关的not exists
查询来检查是否存在&#39 ; t存在同一用户的任何行,具有更高版本的应用程序版本(以及相同的平台)。
这两个查询与第一个不考虑平台的查询完全相同,但如果您需要,您只需将其添加到派生表,分组并加入。
select t1.*
from your_table t1
inner join (
select `user id`, max(`app version`) as max_app_version
from your_table
group by `user id`
) t2
on t1.`user id` = t2.`user id`
and t1.`app version` = t2.max_app_version;
select t1.*
from your_table t1
where not exists (
select 1 from your_table
where `app version` > t1.`app version`
and `user id` = t1.`user id`
and platform = t1.platform
);