从数百万条记录中选择排名

时间:2017-03-30 08:08:05

标签: mysql sql innodb

我会求助于SO,因为我找不到类似的情况,问题/帖子和我的问题

假设我有数百万条记录,则列为
user_id - 假设其记录从1到1,000,000
name - 假设它还记录了最多20个字母的字母
得分 - 0到100,假设它也记录了 日期 - 记录的日期(时间戳)

user_id |   name   | score |        date       |
------------------------------------------------
23131   |   name1  |   15  | 2017-01-04 02:01:25
26824   |   name2  |   63  | 2017-01-04 02:41:33
19684   |   name3  |   28  | 2017-01-04 02:56:15
74937   |   name4  |   01  | 2017-01-04 04:07:55
27486   |   name5  |   75  | 2017-01-04 13:07:45
86476   |   name6  |   56  | 2017-01-04 14:21:47
36479   |   name7  |   19  | 2017-01-04 17:11:15
86752   |   name8  |   38  | 2017-01-04 18:22:23
11267   |   name9  |  100  | 2017-01-04 20:34:42
88763   |   name10 |   89  | 2017-01-04 22:45:43
  1. 我想知道自己的排名,假设我知道我的user_id是什么
  2. 我还想获得我的排名上下的其他10个用户记录,让我们说我的排名是100,我也想选择排名为90到99(高于我的排名)和101到110(低于我的排名)的用户)。
    如果不同的用户按记录日期具有相同的得分顺序排名,则先前的记录具有更高的排名。
  3. 有可能吗?
    假设所有记录都是唯一的,并且没有设置索引。

    我知道如何对排名进行排序

    SELECT * FROM record order by score
    

    但这可以让我选择所有记录,在不选择每条记录的情况下选择特定数据的实用方法是什么?

    这是我想要实现的目标

    user_id |   name   | score |        date          |     rank     |
    ------------------------------------------------------------------
    12341   |   namep  |   90  | 2017-01-01 04:02:36  |      90      |
    45341   |   nameo  |   88  | 2017-01-02 00:05:45  |      91      |
    24341   |   namex  |   88  | 2017-01-03 00:11:15  |      92      |
    26867   |   namec  |   83  | 2017-01-03 01:41:23  |      93      |
    19156   |   nameb  |   81  | 2017-01-03 02:36:45  |      94      |
    74973   |   namem  |   79  | 2017-01-03 04:07:55  |      95      |
    23134   |   namek  |   78  | 2017-01-04 02:01:25  |      96      |
    21424   |   namet  |   77  | 2017-01-04 02:41:33  |      97      |
    19534   |   nameg  |   77  | 2017-01-04 02:56:15  |      98      |
    74912   |   namez  |   75  | 2017-01-04 04:07:55  |      99      |
    
    my_uid  |  my_name |   75  | 2017-01-04 13:07:45  |     100      |
    
    86766   |   namen  |   75  | 2017-01-04 14:21:47  |     101      |
    67976   |   namey  |   74  | 2017-01-04 16:22:23  |     102      |
    34676   |   nameu  |   74  | 2017-01-04 17:33:32  |     103      |
    86236   |   namei  |   73  | 2017-01-04 18:11:09  |     104      |
    98636   |   nameo  |   73  | 2017-01-04 19:21:47  |     105      |
    14326   |   namep  |   73  | 2017-01-04 20:33:22  |     106      |
    45333   |   namet  |   72  | 2017-01-04 20:44:12  |     107      |
    33323   |   namer  |   72  | 2017-01-04 21:34:26  |     108      |
    11322   |   namee  |   71  | 2017-01-04 22:51:54  |     109      |
    86633   |   namew  |   70  | 2017-01-04 22:55:33  |     110      |
    

    好的,所以这就是我现在所得到的,抱歉我没有提到任何关于不使用union或union的事情,我不能在我的项目中使用它。

    但无论如何这里是我的查询 我使用了“multi_query()”函数

    $sql = "SELECT score, date FROM table_name WHERE user_id=your_user_id;" //assume you already know your user_id
    $sql .= "SELECT name, score, date FROM table_name WHERE score >= your_score ORDER BY score, date LIMIT 10;"; //to get 10 rows that have greater or same score of your score order by date, earlier date is higher rank if score is the same with other user.
    $sql .= "SELECT name, score, date table_name WHERE score <= your_score DESC, date ASC LIMIT 10"; //select score less than or equal to my score order by score and date
    

    我得到这样的东西

    my_uid  |  my_name |   75  | 2017-01-04 13:07:45  |     100      |
    
    12341   |   namep  |   90  | 2017-01-01 04:02:36  |      90      |
    45341   |   nameo  |   88  | 2017-01-02 00:05:45  |      91      |
    24341   |   namex  |   88  | 2017-01-03 00:11:15  |      92      |
    26867   |   namec  |   83  | 2017-01-03 01:41:23  |      93      |
    19156   |   nameb  |   81  | 2017-01-03 02:36:45  |      94      |
    74973   |   namem  |   79  | 2017-01-03 04:07:55  |      95      |
    23134   |   namek  |   78  | 2017-01-04 02:01:25  |      96      |
    21424   |   namet  |   77  | 2017-01-04 02:41:33  |      97      |
    19534   |   nameg  |   77  | 2017-01-04 02:56:15  |      98      |
    74912   |   namez  |   75  | 2017-01-04 04:07:55  |      99      |
    
    74912   |   namez  |   75  | 2017-01-04 04:07:55  |      99      |
    my_uid  |  my_name |   75  | 2017-01-04 13:07:45  |     100      |
    86766   |   namen  |   75  | 2017-01-04 14:21:47  |     101      |
    67976   |   namey  |   74  | 2017-01-04 16:22:23  |     102      |
    34676   |   nameu  |   74  | 2017-01-04 17:33:32  |     103      |
    86236   |   namei  |   73  | 2017-01-04 18:11:09  |     104      |
    98636   |   nameo  |   73  | 2017-01-04 19:21:47  |     105      |
    14326   |   namep  |   73  | 2017-01-04 20:33:22  |     106      |
    45333   |   namet  |   72  | 2017-01-04 20:44:12  |     107      |
    33323   |   namer  |   72  | 2017-01-04 21:34:26  |     108      |
    

    我的问题是当使用多个查询时,它仍然与执行3个不同的查询相同,因为我有3个查询,如何将其组合为一个?没有使用工会或工会全部? 在第3个查询中,如何从数据中设置起点?

4 个答案:

答案 0 :(得分:0)

在MySQL中,视图就像是其他语言中的一种功能。

CREATE VIEW rankall AS SELECT * FROM record ORDER BY score;

SELECT * FROM rankall
    WHERE rank > (SELECT rank FROM rankall WHERE user_id = ID) - 11
    AND rank < (SELECT rank FROM rankall WHERE user_id = ID) + 11;

这是基本的想法,但不能保证上面的代码能够起作用:P

答案 1 :(得分:0)

试试这个

SELECT user_id, name, score FROM record
WHERE score BETWEEN 
CONVERT((SELECT score FROM record WHERE user_id = (SELECT user_id FROM record WHERE score = '100') ), INTEGER) - 10
AND
CONVERT((SELECT score FROM record WHERE user_id = (SELECT user_id FROM record WHERE score = '100') ), INTEGER) + 10
ORDER BY score DESC, date DESC

答案 2 :(得分:0)

可能的方法,但有些问题需要解决。这将获得所选用户的任何一方(得分明智)的记录以及该用户的等级。然后按分数命令并计算排名。这确实可以正常工作,但如果所选用户是最高/最低分数,那么会很麻烦。可以对此进行排序,但不确定实际数据是否可以使用。但是会给你一些想法。

这是使用用户ID 86474作为你感兴趣的那个,并且只是为了得到1 - 只是为了适合你给出的测试数据: -

SELECT user_id,
        name, 
        score, 
        date,
        def_rank - (@ranking := @ranking -1) AS rank
FROM
(
    SELECT *
    FROM
    (
        (SELECT r1.user_id,
                r1.name, 
                r1.score, 
                r1.date,
                sub0.def_rank
        FROM record r1
        INNER JOIN record r2 ON r2.user_id = 86476
        CROSS JOIN 
        (
            SELECT COUNT(*) def_rank
            FROM record r1
            INNER JOIN record r2 ON r2.user_id = 86476
            WHERE r1.score >= r2.score
        ) sub0
        WHERE r1.score >= r2.score
        ORDER BY score ASC
        LIMIT 2) 
        UNION
        (SELECT r1.user_id,
                r1.name, 
                r1.score, 
                r1.date,
                sub0.def_rank
        FROM record r1
        INNER JOIN record r2 ON r2.user_id = 86476
        CROSS JOIN 
        (
            SELECT COUNT(*) def_rank
            FROM record r1
            INNER JOIN record r2 ON r2.user_id = 86476
            WHERE r1.score >= r2.score
        ) sub0
        WHERE r1.score <= r2.score
        ORDER BY score DESC
        LIMIT 2)
    ) sub97
    ORDER BY score
) sub1
CROSS JOIN 
(
    SELECT @ranking := 2
) sub2

答案 3 :(得分:0)

我终于得到了我想要的东西,所以我将回答我自己的问题

SELECT score, date FROM rank WHERE uid=your_user_id; //your score and date recorded
SELECT (count(*) + 1) AS rank FROM rank WHERE score > your_score OR (score = your_score AND date < date of your score recorded); //your rank
SELECT * FROM rank WHERE score > your_score OR (score = your_score AND date < date of your score recorded) ORDER BY score ASC, date DESC LIMIT 10; //10 users above my rank, in your output you have to reverse the order
SELECT * FROM rank WHERE score < your_score OR (score = your_score AND date > date of your score recorded) ORDER BY score DESC, date ASC LIMIT 10; //10 users below my rank

感谢其他回复的用户:)