0

我有以下代码,我想根据用户的总余额只获得用户的联赛排名。我怎样才能做到这一点?

// Set League Rank by User
global $post;
$post_author = $post->post_author;
$post_status = 'publish';
$meta_key = 'balance';
$rank = $wpdb->get_var( $wpdb->prepare(
    "
    SELECT *, @rownum := @rownum + 1 from
    (
    SELECT 
        SUM(meta_value) as balance, 
    FROM {$wpdb->postmeta} pm
    INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID
    JOIN    (SELECT @curRow := 0) r;
    WHERE pm.meta_key = %s AND p.post_author = %s AND p.post_status = %s
    order by balance desc)x, (SELECT @rownum := 0) r
    ",
    $meta_key, $post_author, $post_status
    ) );

有人可以帮助我吗?

谢谢你。

4

1 回答 1

0

尝试这样的事情:

SELECT 
    ROUND (SUM(meta_value),2) as balance, 
    @curRow := @curRow + 1 AS rank
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID
JOIN    (SELECT @curRow := 0) r;
WHERE pm.meta_key = %s AND p.post_author = %s AND p.post_status = %s
order by balance desc

我可能误解了你所说的排名是什么意思,所以如果这不起作用,请进一步解释你到底需要什么。

于 2013-05-14T15:20:10.303 回答