按声誉对表格进行排序,并选择排名+ LIMIT以打印行上方和下方

时间:2013-07-05 11:26:54

标签: php mysql

我正在尝试按声誉对表格进行排序,然后选择行[rank]的位置,它工作正常,但我想打印出上面的3个表格和3个表格在这个等级行下面,但LIMIT排名,6不起作用:/(我很抱歉格式错误的SQL:/)会感谢每一个帮助:)

以下是有效的方法:

$query  = mysqli_query($con, "SET @rank=0");
$query  = mysqli_query($con, "
            SELECT 
               rank, 
               user_email, 
               reputation 
            FROM (
               SELECT 
                 @rank:=@rank+1 AS rank, 
                 user_email, 
                 reputation 
               FROM accounts, (SELECT @rank := 0) r 
               ORDER BY 
                 reputation DESC 
                 LIMIT 0,7
            ) t 
            WHERE 
               reputation >= '5' 
               OR reputation < '5'");

打印

[rank] [user_email]       [reputation]
1      mail1@gmail.com    20
2      test@test.com      15
3      mail2@gmail.com    10
4      othermail@gmail.com 5
5      hmmmmm@gmail.com    0
6      ouch@gmail.com      0
7      somemail@gmail.com  0

1 个答案:

答案 0 :(得分:1)

我认为你找到了声望为5的人,并希望那个人加上上下两个人的等级。

在这种情况下,可能选择,因为你基本上必须得到声誉为5的人的等级,然后加入反对相同的选择重复找到那些具有等级的任何一方: -

SELECT Sub1.rank, Sub1.user_email, Sub1.reputation 
FROM 
(
    SELECT @rank2:=@rank2+1 AS rank, user_email, reputation 
    FROM accounts, (SELECT @rank2 := 0) r 
    ORDER BY reputation DESC 
    LIMIT 0,7
) Sub1 
INNER JOIN
(
    SELECT rank, user_email, reputation 
    FROM 
    (
        SELECT @rank:=@rank+1 AS rank, user_email, reputation 
        FROM accounts, (SELECT @rank := 0) r 
        ORDER BY reputation DESC 
        LIMIT 0,7
    ) t 
    WHERE reputation = '5'
) Sub2
ON Sub1.rank BETWEEN (Sub2.rank - 2) AND (Sub2.rank + 2)