我有一张表如何才能获得最后3行进行比较并得到计数?
示例:表
id| name
=========
1 | Apple
2 | Orange
3 | Kiwi
4 | Banana
5 | Apple
6 | Banana
7 | Apple
如何获得最后3行的Apple计数?
非常感谢
答案 0 :(得分:1)
你的问题不明确,但如果我理解正确的话; 检查查询和结果:
SELECT
id, name, COUNT(*) AS total_row
FROM
furits
GROUP BY NAME ORDER BY id DESC LIMIT 0,3;
id| name | total_row
======================
4 | Banana | 2
3 | Kiwi | 1
2 | Orange | 1
SELECT
id, name, COUNT(*) AS total
FROM
furits
GROUP BY NAME ORDER BY id DESC;
id| name | total_row
======================
4 | Banana | 2
3 | Kiwi | 1
2 | Orange | 1
1 | Apple | 3
SELECT
id, name, COUNT(*) AS total_row
FROM
furits
WHERE
name = 'Apple'
GROUP BY NAME LIMIT 0,1;
id| name | total_row
======================
1 | Apple | 3
我希望这有帮助。
答案 1 :(得分:1)
您可以使用临时表来选择最后3行,使用结果行
select name, count(*) from
(select name,id
from my_table
order by id desc limit 3) t
group by name