Problems with a MYSQL query - Categories and Items

Asked

Viewed 81 times

8

I’m doing a project, which basically consists of item and category... or better "animes" and "episodes"...

Here’s the problem, I want to list all the registered anime and a count of the episodes related to them, for example. " Katekyo - 24 episodes"... that I got, but in the list only comes the anime that are related to them, the anime that are not with episodes do not appear in the query!

Currently

Table Animes

id_anime   nome_anime   capa_anime   visualizacoes_anime
1          Anime1       capa.png     250
2          Anime2       capa.png     250
3          Anime2       capa.png     250

Episode Table

id_episodio   numero_episodio   capa_episodio   visualizacoes_episodio   id_anime
1             Episodio1         capa.png        250                      1
2             Episodio2         capa.png        250                      1
3             Episodio2         capa.png        250                      2

Consultation

SELECT nome_anime, animes.id_anime as id_do_anime, COUNT(id_episodio) as quantidade_epis 
FROM animes INNER JOIN episodios_animes ON animes.id_anime = episodios_animes.id_anime 
GROUP BY episodios_animes.id_anime

As a result

nome_anime   id_do_anime   quantidade_epis
Anime1       1             2
Anime2       2             1

Problem is that only those with episodes counting, the anime 3 that has no episode did not come on the list, and I wanted it to come all, and if there is no episode in it, come a zero in the case!

1 answer

5


Replace the INNER JOIN of your query by a LEFT JOIN.

When there is no value in the table to the right, it will still bring the result of the table to the left with the null values to the right.

To bring ordered as last inserted episodes, just make a ORDER BY episodios_animes.id_episodio DESC, once the last entered will have the largest id’s.

SELECT
  nome_anime,
  animes.id_anime as id_do_anime,
  COUNT(id_episodio) as quantidade_epis 
FROM animes
LEFT JOIN episodios_animes ON animes.id_anime = episodios_animes.id_anime 
GROUP BY animes.id_anime
ORDER BY episodios_animes.id_episodio DESC

See working on sqlfiddle

  • Thanks for answering... next, I switched to LEFT, but instead of I came all anime, came the animes that are with the episodes and only came 1 with the value 0... in total has 23 anime.

  • 1

    @Viniciusvieira try to change his GROUP BY for animes.id. Can make http:/sqlfiddle.com available/ ?

  • It worked my dear, thank you... there goes fiddle http://sqlfiddle.com/#! 2/de4f8a/1 I have just one more question, how to list anime on showing on top the last ones that have been added episodes in them... type anime5 25 episodes anime2 29 episodes anime1 0 episodes anime4 0 episodes Thank you

  • 1

    Here it works as Vinicius said: http://sqlfiddle.com/#! 2/c75d01/2

  • 2

    @Luishenrique added sqlfiddle in his reply. If you don’t like revert the edition.

  • Thanks also Jorge for the help, thanks this working all beauty!

  • 1

    No problem @Jorgeb. + 1 for collaboration.

  • 1

    @Viniciusvieira I edited the query in my reply with your ordering request, F5 and.

  • Look at that beauty, it’s top! thank you very much! saved my life lot! ^^

  • ai worked mass... http://i.imgur.com/juKEast.png

Show 5 more comments

Browser other questions tagged

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