如何根据不同的值对MySQL结果进行排名? [英] How to rank MySQL results, based on different values?

查看:155
本文介绍了如何根据不同的值对MySQL结果进行排名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有2个不同的表,名称分别是:等级,设置.

I have 2 different tables in my database by the name of: rank, settings.

这是每个表的样子,其中有一些记录:

Here is how each table looks like with a few records in them:

id  points userid
--  ----- ------
 1    500      1
 2    300      2    
 3    900      3
 4   1500      4
 5    100      5
 6    700      6
 7    230      7
 8    350      8
 9    850      9
10    150     10

表#设置:

userid    active 
------    ------
     1         0
     2         1
     3         1
     4         1
     5         1
     6         0
     7         1
     8         1
     9         0
    10         1

我基本上要实现的是按ID从#rank中选择特定行,按点对其进行排序,然后选择特定ID上方的3行和特定ID下方的3行,但仅针对活动列(从#settings)等于1.

What I basically want to achieve is to select a specific row from #rank by ID, sort it by points and select 3 rows above the specific ID and 3 row below the specific ID but only for rows where the active column (from #settings) for the user equals 1.

例如: 我想从#rank中选择ID为8,它应该返回以下内容:

For example: I would like to select from #rank the ID of 8, and it should return me the following:

rank points userid
---- ----- ------
   2   150     10   
   3   230      7
   4   300      2
   5   350      8
   6   900      3
   7   1500     4

我为此创建了一个广泛的查询,但是问题是,它在确定用户是否处于活动状态之前对列进行了排名.但是,在确定用户是否处于活动状态之后,我需要对列进行排名.

I have created quite an extensive query for this, but the problem is, that it is ranking the columns before it decides that the user is active or not. However I need to rank the columns after it is decided that the user is active or not.

SELECT  sub2.sort, sub2.points, sub2.userid
FROM
(
    SELECT  @sort1 := @sort1 + 1 AS sort, puu.points, puu.userid
    FROM    rank as puu,
    (SELECT @sort1 := 0) s
    LEFT JOIN 
    (
        settings as p11 
    )
    ON puu.userid = p11.userid,
    WHERE p11.active = 1
    ORDER BY puu.points DESC
) sub1
INNER JOIN
(
    SELECT @sort2:=@sort2+1 AS sort, p2.points, p2.userid 
    FROM rank as p2,
    (SELECT @sort2 := 0) s
            LEFT JOIN
    (
            settings as p12
    ) 
    ON p2.userid = p12.userid,
    WHERE p12.active = 1
    ORDER BY points DESC
) sub2
ON sub1.userid = :userid
AND sub2.sort BETWEEN (sub1.sort - 5) AND (sub1.sort + 5)

你们能为我的问题找到任何解决方案吗?如果您可以提供一个SQLfiddle演示,那真是太棒了!

Can you guys find any solution for my problem? If you can provide an SQLfiddle demo, that would be really awesome!

推荐答案

SELECT sort, points, user_id, active FROM (
    SELECT @pos := @pos + 1 AS sort, id, points, r.user_id, s.active,
    IF(user_id = :userid, @userpos := @pos, 0)
  FROM rank r
  JOIN settings s USING(user_id)
  JOIN (SELECT @pos := 0, @userpos := 0) p
  WHERE s.active = 1
  ORDER BY points DESC
) list
WHERE sort BETWEEN @userpos - 3 AND @userpos + 3

我在这里做了一个小提琴: sqlfiddle

I made a fiddle here: sqlfiddle

这篇关于如何根据不同的值对MySQL结果进行排名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆