我想限制组内记录的大小,这是我的试用版,如何正确操作?
mysql> select * from accounts limit 5 group by type;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group by type' at line 1
最佳答案
聚合函数(以及它需要的 GROUP BY)的要点是将多行变成一行。因此,如果您真的只想要前 5 个储蓄账户和前 5 个支票账户以及前 5 个美元账户等,您需要的更像是这样:
标准:特定账户类型的前 5 名账户余额
SELECT account_type, account_balance FROM accounts WHERE account_type='savings'
ORDER BY account_balance DESC LIMIT 5
UNION
SELECT account_type, account_balance FROM accounts WHERE account_type='chequing'
ORDER BY account_balance DESC LIMIT 5
UNION
SELECT account_type, account_balance FROM accounts WHERE account_type='USD'
ORDER BY account_balance DESC LIMIT 5;
这不是很漂亮,但是如果您使用脚本构建 SQL,然后代入 account_types 并将查询串联在一起就很简单。
关于组内的mysql限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1153738/