gpt4 book ai didi

mysql - 合并数百万数据库中的多行

转载 作者:行者123 更新时间:2023-11-29 19:39:59 24 4
gpt4 key购买 nike

我有一个包含数百万行的表,其结构如下:

UserID | Date       | Points
1 | 2016-05-01 | 240
1 | 2016-05-02 | 500
1 | 2016-05-03 | 650
2 | 2016-05-01 | 122
2 | 2016-05-02 | 159
2 | 2016-05-03 | 290

等等等等。

我需要找出每个 ID 的 2016-05-032016-05-01 之间的 Points 差异,排序依据Points 的差异,并返回前 100 名。我的数据库包含数亿行,因此需要快速操作。

我该从哪里开始呢?我正在查看 group_concat 但我不确定这是否是此用例的正确函数。

最佳答案

您应该有如下所示的子查询

SELECT 
user_id,
(select points from tbl_test where tbl_test.date_of='2016-05-03' and user_id=t1.user_id)-t1.points as difference_in_points
FROM `tbl_test` as t1
WHERE date_of='2016-05-01' order by difference_in_points desc
limit 100

关于mysql - 合并数百万数据库中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41413798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com