mysql在记录集中查找位置但从另一个表中获取值

时间:2017-07-13 09:59:17

标签: mysql sql

我有这个sql从players记录集中获取单个玩家的财富排名(位置)。但我需要加入users表,以便在找到位置时忽略被禁止的玩家。

我现有的代码(检查玩家ID 5):

SELECT z.rank FROM 
    (SELECT p.id, p.name, @rownum := @rownum + 1 AS rank FROM players p, (SELECT @rownum := 0) r 
    WHERE p.this != 'that' ORDER BY p.cash + p.bank DESC) as z #
    WHERE id=5;

所以我需要的是这样的东西:(这不起作用)

SELECT z.rank FROM 
    (SELECT p.id, p.name, 
        (SELECT ban FROM u.users WHERE id=p.account_id) as banned, @rownum := @rownum + 1 AS rank FROM players p, (SELECT @rownum := 0) r 
    WHERE p.this != 'that' AND banned=0 ORDER BY p.cash + p.bank DESC) as z #
    WHERE id=5;

对我来说太复杂了!

0 个答案:

没有答案