mysql limit inside group?

后端 未结 8 667
粉色の甜心
粉色の甜心 2021-01-13 07:35

I want to limit the size of records inside a group, and here is my trial, how to do it right?

mysql> select * from accounts limit 5 group by type;
         


        
8条回答
  •  迷失自我
    2021-01-13 07:41

    @dnagirl's answer almost has it, but for some reason, my version of MySQL only returns the first LIMIT'd set. To get around that, I put each statement into a subquery

    SELECT * FROM (
        SELECT account_type, account_balance FROM accounts WHERE account_type='savings' 
           ORDER BY account_balance DESC LIMIT 5
    ) as a
    UNION
    SELECT* FROM (
        SELECT account_type, account_balance FROM accounts WHERE account_type='chequing' 
           ORDER BY account_balance DESC LIMIT 5
    ) as b
    UNION
    SELECT * FROM (
        SELECT account_type, account_balance FROM accounts WHERE account_type='USD' 
           ORDER BY account_balance DESC LIMIT 5
    ) as c
    

    This gave me back each set's results in the final result set. Otherwise, I would have only gotten the first 5 from the first query and nothing else - not sure if it's just some MySQL funk with my version

提交回复
热议问题