我想限制组内记录的大小,这是我的试用版,如何做到对不对?
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;
它并不漂亮,但如果您使用脚本构造SQL,则在account_types中进行子搜索并将查询连接在一起是很简单的.