Skip to content
Advertisement

Group by not getting the expected results in mysql

I have the next query:

That is working perfectly and returning 9 rows:

I want to group the results by boat type and get the number of boats per type.

However, when I do:

I´m getting:

but according to the first query, I´m expecting

What am I missing?

Advertisement

Answer

I suspect that you want:

That is: move the DISTINCT within the COUNT() rather than directly in the SELECT.

Generally speaking, DISTINCT and GROUP BY do not go along well together; DISTINCT is already aggregation in essence, so mixing both is usually not relevant.

Note that your syntax uses old-school, implicit joins (with a comma in the FROM clause): you should be using standard joins (with the ON keyword), whose syntax has been state-of-the-art for decades.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement