Blog Emka

Portal linux, pemrograman sistem informasi, computer security & santri

Tutorial PHP

Tutorial MySQL: How to Group By Two or More Column in MySQL!

So you trying to group data by multiple column here and you’re using MySQL? There is no native way to do that, you have to combine ‘Group By’ syntax with other else.

Regular Group by would be like this:

select client_id, name, max(cash + stocks)
from client join portfolio using (client_id)
group by client_id

+-----------+----------+--------------------+
| client_id | name     | max(cash + stocks) |
+-----------+----------+--------------------+
|         1 | John Doe |              33.33 | 
|         2 | Jane Doe |              90.90 | 
+-----------+----------+--------------------+

to group by two column, you need to concat those two column first, your syntax will be like this:

select client_id, name, max(cash + stocks)
from client join portfolio using (client_id)
group by concat(client_id,name)

Understand now?

God bless your SQL…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Theme by Anders Norén

%d bloggers like this: