如何合并以下两个查询以向我提供具有总体排名的特定用户事件?
查询1.根据投票数返回事件的排名。
SELECT e.guid, e.name, e.votes, @curRank := @curRank + 1 AS rank
FROM event e, (SELECT @curRank := 0) r
ORDER BY votes DESC;
查询2.返回特定用户的事件。
SELECT e.guid, e.name, e.votes
FROM user_event ue
JOIN event e
ON e.guid = ue.event_uid
WHERE ue.user_uid = '123';
活动表
guid | name | votes
-------------------
abc event1 10
def event2 5
ghi event3 15
用户事件表
user_event_id | event_guid | user_uid
--------------------------------------
1 abc 123
2 def 123
3 ghi 456
组合查询应返回user123
guid name votes rank
abc | event1 | 10 | 2
def | event2 | 5 | 3
或者对于用户456:
guid name votes rank
ghi | event3 | 15 | 1
答案 0 :(得分:1)
这适用于您的版本:
SELECT r.* FROM user_event ue
JOIN(
SELECT e.guid, e.name, e.votes, @curRank := @curRank + 1 AS rank
FROM event e, (SELECT @curRank := 0) r
ORDER BY votes DESC
) AS r ON r.guid = ue.event_guid
WHERE ue.user_uid = 123