我想限制组内记录的大小,这是我的试用版,如何做到对不对?
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个美元账户等,您需要的更像是:
条件:account_balance的特定帐户类型的前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;