Answer the question
In order to leave comments, you need to log in
How to find out the sequence number of a record from a selection of two tables?
Request type
SELECT t1.target_id, SUM(t1.cnt) AS hm, t2.id, t2.name FROM votes AS t1, people AS t2 WHERE t1.target_id=t2.id GROUP BY t2.id ORDER BY hm DESC, t2.name ASC LIMIT 10
@row:[email protected]+ 1
Answer the question
In order to leave comments, you need to log in
We need to wrap the query in another SELECT
SET @row = 0;
SELECT *, @row := @row + 1 AS `pos`
FROM (
SELECT `t1`.`id`, `t1`.`hm`, `t2`.`name`
FROM (
SELECT `target_id` AS `id`, SUM(`cnt`) AS `hm`
FROM `votes`
GROUP BY `target_id`
) AS `t1`
JOIN `people` AS `t2` ON `t2`.`id` = `t1`.`id`
ORDER BY `t1`.`hm` DESC, `t2`.`name` ASC
LIMIT 10
) AS `x`
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question