MySQL GROUP BY two columns

后端 未结 2 1787
礼貌的吻别
礼貌的吻别 2020-12-04 07:37

I\'m trying to group by multiple columns here - one on each table.
It\'s a scenario where I want to find the top portfolio value for each client by adding their current

2条回答
  •  盖世英雄少女心
    2020-12-04 07:50

    Using Concat on the group by will work

    SELECT clients.id, clients.name, portfolios.id, SUM ( portfolios.portfolio + portfolios.cash ) AS total
    FROM clients, portfolios
    WHERE clients.id = portfolios.client_id
    GROUP BY CONCAT(portfolios.id, "-", clients.id)
    ORDER BY total DESC
    LIMIT 30
    

提交回复
热议问题