[英]How to get rank according to count in mysql
I am getting issue with fetching the rank according to the count. 我在根据计数获取排名时遇到问题。
I have two tables : 我有两个表:
1)images
2)images_like
Now if the images_like have no entries then it is showing "NULL" in the ranks. 现在,如果images_like没有条目,则其排名中将显示“ NULL”。 please check the query and output :
请检查查询并输出:
SELECT im.id,
rank
FROM (SELECT image_id,
@rownum := @rownum + 1 AS rank,
Count(*) AS cnt
FROM images_like,
(SELECT @rownum := 0) r
GROUP BY image_id
ORDER BY `cnt` DESC) AS d
RIGHT JOIN images AS im
ON d.image_id = im.id
i need to display 3 not null .... 我需要显示3不为空....
Thanks in advance !!!!! 提前致谢 !!!!!
Try like this : 尝试这样:
SELECT im.id, d.rank
FROM
(
SELECT t.image_id, @rownum := @rownum + 1 AS rank
FROM
(
SELECT image_id,
Count(*) AS cnt
FROM images_like
GROUP BY image_id
ORDER BY `cnt` DESC) AS d
) t,
(SELECT @rownum := 0) r
) d
RIGHT JOIN images AS im ON d.image_id = im.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.