Assume user table has 200 records.
The query below works for a certain worker_id For example worker_id 2738:
开发者_C百科SELECT SUM(something) AS T_BOIN
FROM (
SELECT SUM(A.boin) AS something
FROM hall A JOIN user B ON A.who=B.id
WHERE B.worker_id = 2738
ORDER BY total_per_user DESC LIMIT 10
) AS mastertbl
I want to execute the above query for 200 seperate worker_ids like this:
SELECT @TT:=BK.worker_id AS TID,
(SELECT SUM(something) AS T_BOIN
FROM (
SELECT SUM(A.boin) AS something
FROM hall A
JOIN user B ON A.who=B.id
WHERE B.worker_id = @TT
ORDER BY total_per_user DESC
LIMIT 10) AS mastertbl
) AS TEAM
FROM user BK
INNER JOIN (SELECT @TT :=0) AS WESS
GROUP BY TID
But @tt is out of scope.
Thanks in advance.
I think this may work for you...
SELECT worker_id, SUM(something) AS T_BOIN
FROM (
SELECT b.worker_id, SUM(A.boin) AS something, if(@wid = b.worker_id, @cnt:=@cnt+1, @cnt:=1 AND @wid := b.worker_id) cnt
FROM hall A
JOIN user B ON A.who=B.id
ORDER BY b.worker_id, total_per_user DESC
) AS mastertbl
WHERE mastertbl.cnt <= 10
GROUP BY worker_id;
精彩评论