SELECT in MYSQL grouped by month, but showing all

Asked

Viewed 215 times

1

I have the following table in MYSQL:

cavalo - 2016-01-15
cachorro - 2016-01-16
gato - 2016-02-23
coelho - 2016-02-24
ovelha - 2016-03-21
tatu - 2016-03-22

I wanted to give a SELECT and show that way:

JANEIRO - 2016: cavalo cachorro

FEVEREIRO - 2016: gato coelho

MARÇO - 2016: ovelha tatu

I even tried with GROUP BY, but then he doesn’t list all of each month, someone has a light?

  • ORDER BY YEAR(data), MONTH(data) would be sufficient. If you are doing this via some extra language (PHP, for example) it pays to use ORDER BY and just format in the view.

  • To using PHP. ?

  • Exact. outside the loop, starts $mes_current='''; inside the loop: if $db_mes == $mes_current shows only the bug, otherwise $mes_current = $db_mes and shows everything. Little code, easy to read, no complication. If you have any difficulty, put the current loop in the question and the PHP tag, which I put an example.

  • Here’s something like (yours is even simpler): http://answall.com/questions/105060/

  • You can "denormalize" this table... but it all depends on how many animals you have.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.