如何选择 user_ids 按B meta_key和按评分排序 meta_value?
user_id | meta_key | meta_value ----------------------------------------------------- 1 A 1 1 B 1 1 C 1 1 rating 9 8 A 1 8 C 1 8 rating 99999 7 A 1 7 B 1 7 C 1 7 rating 999
我需要获取具有 B meta_key的用户的ID,然后按用户评级值订购ID。
答案 0 :(得分:1)
试试这个:
SELECT T1.user_id
FROM yourtable T1
JOIN yourtable T2
ON T1.user_id = T2.user_id
AND T2.meta_key = 'rating'
WHERE T1.meta_key = 'B'
ORDER BY CAST(T2.meta_value AS SIGNED)
答案 1 :(得分:1)
为此,您需要自我加入:
select t.*
from t left outer join
(select t.user_id, max(meta_value) as rating
from t
where t.meta_key = 'rating'
group by t.user_id
) r
on t.user_id = r.user_id
where meta_key = 'B'
order by r.rating