SepDev
SepDev

Reputation: 153

MySQL distinct in 2 rows

I got something like this database structure:

2017 | February
2017 | February
2018 | February
2019 | February

Output should be:

2017 | February
2018 | February 
2019 | February

Is it possible to get this to work with an distinct argument?

Upvotes: 1

Views: 55

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

An alternative to using DISTINCT would be to use GROUP BY and do an aggregation:

SELECT year, month
FROM yourTable
GROUP BY year, month

From the MySQL documentation we find that DISTINCT is often implemented internally using GROUP BY:

In most cases, a DISTINCT clause can be considered as a special case of GROUP BY.

Most of the time, GROUP BY would be preferable because it allows us to use aggregate functions while DISTINCT does not.

Upvotes: 1

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

Yes, just do:

select distinct year, month
  from your_table

Upvotes: 2

Mureinik
Mureinik

Reputation: 311326

Just apply the distinct modifier to your query:

SELECT DISTINCT `year`, `month`
FROM   mytable

Upvotes: 2

Related Questions