我正在尝试做类似Select the 3 most recent records where the values of one column are distinct的工作,但带了一个计数,这意味着我需要保留该组。MYSQL Group Gy订购Count?
使用相同的例子:
id time text otheridentifier
-------------------------------------------
1 6 apple 4
2 7 orange 4
3 8 banana 3
4 9 pear 3
5 10 grape 2
SELECT *, COUNT(*) FROM `table` GROUP BY (`otheridentifier`) ORDER BY `time` DESC LIMIT 3
将返回5号,3,1与适当的罪状,但我想5,4,2
在该职位的解决方案是
SELECT * FROM 'table' WHERE 'id' = (SELECT 'id'
FROM 'table' as 'alt'
WHERE 'alt'.'otheridentifier' = 'table'.'otheridentifier'
ORDER BY 'time' DESC
LIMIT 1) ORDER BY 'time' DESC LIMIT 3
,但我看不出如何抓住计数,因为我需要一个分组有:我得到ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
如果我添加COUNT(*),以那开始。
您的sql中没有任何组。你想要计算什么? – 2011-04-19 19:59:14
我在第一个查询中做了:我试图计算由'otheridentifier'分组的项目数。 – aaronwinborn 2011-04-19 20:04:48