MySQL为使用排名的平局添加字符串“T

xbp102n0  于 2022-09-18  发布在  Java
关注(0)|答案(1)|浏览(126)

我有以下MySQL查询,它可以按预期工作。

SELECT DISTINCT u.id,
CASE WHEN a.result IS NULL THEN 0 ELSE a.result END 'result',
RANK() OVER(ORDER BY result DESC) as 'rank'
FROM wp_users u, wp_usermeta um,
(SELECT pm.meta_value, COUNT(*) result
FROM wp_postmeta pm, wp_posts p
where p.id = pm.post_id
and p.post_status = 'publish'
and pm.meta_key = 'wpcf-userid'
and pm.post_id in (select pm1.post_id from wp_postmeta pm1 where pm1.meta_key = 'wpcf-badge-name' and pm1.meta_value = 'LD' and pm1.post_id = pm.post_id) 
and pm.meta_value IN (select um.user_id FROM wp_usermeta um WHERE um.meta_value = 'VIC' and um.meta_key = 'wpcf-address-state')
GROUP BY pm.meta_value) a
WHERE u.id = um.user_id and a.meta_value = u.id
group by u.id

上面的结果是

Id|结果|排名
-|-|
2629|5|1
2783|5|2
817|4|3
2509|4|3
5363|3|5

我想要在任何领带的排名之前添加“T”字符串,如下所示:

Id|结果|排名
-|-|
2629|5|1
2783|5|2
817|4|T3
2509|4|T3
5363|3|5

有什么建议吗?

t98cgbkg

t98cgbkg1#

我们可以子查询,然后使用COUNT()作为分析函数来检测排名中的平局:

WITH cte AS (
    SELECT u.id,
           CASE WHEN a.result IS NULL THEN 0 ELSE a.result END AS result,
           RANK() OVER(ORDER BY result DESC) AS `rank`
    FROM wp_users u, wp_usermeta um,
         (SELECT pm.meta_value, COUNT(*) result
          FROM wp_postmeta pm
          INNER JOIN wp_posts p ON p.id = pm.post_id
          WHERE p.post_status = 'publish' AND
                pm.meta_key = 'wpcf-userid' AND
                pm.post_id IN (SELECT pm1.post_id FROM wp_postmeta pm1
                               WHERE pm1.meta_key = 'wpcf-badge-name' AND
                                     pm1.meta_value = 'LD' AND
                                     pm1.post_id = pm.post_id) AND
                pm.meta_value IN (SELECT um.user_id FROM wp_usermeta um
                                  WHERE um.meta_value = 'VIC' AND
                                        um.meta_key = 'wpcf-address-state')
          GROUP BY pm.meta_value) a
    WHERE u.id = um.user_id AND a.meta_value = u.id
    GROUP BY u.id
)

SELECT id, result,
       CASE WHEN COUNT(*) OVER (PARTITION BY `rank`) = 1
            THEN `rank` ELSE CONCAT('T', `rank`) END AS `rank`
FROM cte t
ORDER BY t.`rank`;

相关问题