Here is my request:
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, which, COUNT(*) AS cnt
FROM
(
SELECT rid, rootid, 'vote' which FROM p_likes
UNION ALL
SELECT rid, rootid, 'comment' which FROM p_comments
UNION ALL
SELECT rid, rootid, 'friend' which FROM relations
) y
WHERE y.rootid = 1246 AND y.rootid <> y.rid
GROUP BY y.rid, y.rootid, y.which
) x
INNER JOIN pagesroot pr1 on x.rootid = pr1.id
INNER JOIN pagesroot pr2 on x.rid = pr2.id
ORDER BY x.cnt desc;
And here is the query output above: ( //means the value of this entry is not related to this question.)
+---------+-----------+----------+------------+-------------+-------+
| user_id | user_name | liker_id | liker_name | which_table | total |
+---------+-----------+----------+------------+-------------+-------+
|
|
|
|
|
|
|
|
|
|
|
|
+---------+-----------+----------+------------+-------------+-------+
All I'm trying to do is sort the strings alternatively. As you can see, I am currently ordering results based on a column total. Although I need to sort them both based totaland which_table. Something like this: (expected result)
+---------+-----------+----------+------------+-------------+-------+
| user_id | user_name | liker_id | liker_name | which_table | total |
+---------+-----------+----------+------------+-------------+-------+
|
|
|
|
|
|
|
|
|
|
|
|
+---------+-----------+----------+------------+-------------+-------+
How can i do this?
source
share