rid, rootid, which. rid, rootid, which, COUNT(*) AS cnt, rank. , rid, rootid, which rid, rootid, which. rid, rootid, which , ? rid, rootid, which 3, 6, 9, , , ? ( MIN MAX), . , .
, 10 ( rid rootid 1246, ). rootid = 1246 and rootid <> rid . LIMIT, .
SELECT
pr1.id AS user_id,
pr1.title AS user_name,
pr2.id AS liker_id,
pr2.title AS liker_name,
x.which AS which_table,
x.cnt AS total
FROM
(
(
SELECT rid, rootid, 'vote' AS which, COUNT(*) AS cnt
FROM p_likes
WHERE rootid = 1246 and rootid <> rid
ORDER BY COUNT(*) DESC
LIMIT 10
)
union all
(
SELECT rid, rootid, 'comment' AS which, COUNT(*) AS cnt
FROM p_comments
WHERE rootid = 1246 and rootid <> rid
ORDER BY COUNT(*) DESC
LIMIT 10
)
union all
(
SELECT rid, rootid, 'friend' AS which, COUNT(*) AS cnt
FROM relations
WHERE rootid = 1246 and rootid <> rid
ORDER BY COUNT(*) DESC
LIMIT 10
)
) x
INNER JOIN pagesroot pr1 ON x.rootid = pr1.id
INNER JOIN pagesroot pr2 ON x.rid = pr2.id
ORDER BY x.cnt DESC, x.which;