又臭又长的SQL

根据积分数范围计算用户等级

select views_level,count(*) as count from
(
    select elt(interval(tba.views,0, 501, 1001, 2001, 3501, 5001, 7501), '7', '6', '5', '4', '3', '2', '1') as views_level
    from (
        SELECT p.ID as id,pm.meta_value as views FROM `wp_posts` as p
        left join `wp_postmeta` as pm on p.ID = pm.post_id
        WHERE pm.meta_key = 'views'
        AND p.post_author = %d
        and p.post_type = 'post'
        and p.post_status = 'publish'
        and p.ID not in(SELECT post_id FROM `score_spread` where step = 1)
    ) as tba
) as tbb
group by views_level order by views_level asc

点赞

发表评论

邮箱地址不会被公开。 必填项已用*标注