Wednesday, July 16, 2014

select distinct for composite key in MySQL using GROUP BY




For example,we have users table and containing firstname and lastname.
We can select distinct lastname using:
SELECT DISTINCT lastname FROM users
What happen if we want to see distinct of combination of firstname and lastname?
We can use GROUP BY as following
SELECT lastname, firstname  FROM users GROUP BY lastname, firstname

No comments:

Post a Comment