0

我有一个 MySQL 查询,我想在其中添加一个列来根据用户的余额对用户进行排名。查询是:

SELECT (display_name) 'Author',
IFNULL(ROUND(SUM(balance.meta_value),2),2) 'Balance',
IFNULL(ROUND((((SUM(balance.meta_value+bet.meta_value)-                                                         
SUM(bet.meta_value))/SUM(bet.meta_value))*100),2),2) 'Yield %'
FROM wp_posts p
 JOIN wp_users u 
ON p.post_author = u.ID
 LEFT JOIN wp_postmeta bet 
ON p.ID = bet.post_id AND bet.meta_key = 'bet'
LEFT JOIN wp_postmeta balance 
ON p.ID = balance.post_id AND balance.meta_key = 'balance'
WHERE p.post_status = 'publish'
GROUP BY u.ID
ORDER BY Balance DESC

我尝试添加SET @rownum := 0; and @rownum := @rownum + 1AS 排名,但它不起作用,排名不是按最高余额排序的。

有人能帮我吗。谢谢。

4

1 回答 1

3
select *, @rownum := @rownum + 1 from
(
SELECT (display_name) 'Author',
    IFNULL(ROUND(SUM(balance.meta_value),2),2) 'Balance',
    IFNULL(ROUND((((SUM(balance.meta_value+bet.meta_value)-SUM(bet.meta_value))/SUM(bet.meta_value))*100),2),2) 'Yield %'
FROM wp_posts p
JOIN wp_users u 
    ON p.post_author = u.ID
LEFT JOIN wp_postmeta bet 
    ON p.ID = bet.post_id AND bet.meta_key = 'bet'
LEFT JOIN wp_postmeta balance 
    ON p.ID = balance.post_id AND balance.meta_key = 'balance'
WHERE p.post_status = 'publish'
GROUP BY u.ID
ORDER BY Balance DESC
)x, (select @rownum := 0) r
于 2013-05-14T11:24:38.293 回答