我在名为消息(id,category,user)的表上有这些数据:
1 1 aaa
2 2 aaa
3 3 aaa
4 1 bbb
5 2 aaa
6 1 ccc
7 2 aaa
8 3 ddd
我只想返回一行,类别1的行数(在示例中 3 )和该类别的最后一个ID(在示例中 6 )。
如何使用MySql查询执行此操作?
答案 0 :(得分:4)
select count(*), max(id) from mytab where category = 1
答案 1 :(得分:2)
SELECT category, COUNT(category) AS counter, MAX(id) AS max FROM messages
GROUP BY category
答案 2 :(得分:0)
SELECT COUNT(id), MAX(id) FROM tblName
WHERE category = 1
GROUP BY category