组内的mysql限制?

前端之家收集整理的这篇文章主要介绍了组内的mysql限制?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想限制组内记录的大小,这是我的试用版,如何做到对不对?

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中进行子搜索并将查询连接在一起是很简单的.

猜你在找的MySQL相关文章