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!
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.
– Vinicius Vieira
@Viniciusvieira try to change his
GROUP BY
foranimes.id
. Can make http:/sqlfiddle.com available/ ?– Luis Henrique
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
– Vinicius Vieira
Here it works as Vinicius said: http://sqlfiddle.com/#! 2/c75d01/2
– Jorge B.
@Luishenrique added sqlfiddle in his reply. If you don’t like revert the edition.
– Jorge B.
Thanks also Jorge for the help, thanks this working all beauty!
– Vinicius Vieira
No problem @Jorgeb. + 1 for collaboration.
– Luis Henrique
@Viniciusvieira I edited the query in my reply with your ordering request, F5 and.
– Luis Henrique
Look at that beauty, it’s top! thank you very much! saved my life lot! ^^
– Vinicius Vieira
ai worked mass... http://i.imgur.com/juKEast.png
– Vinicius Vieira