Cześć, mam problem, z którym walczę już chyba 2gą godzinę i nic nie wychodzi.
Jest tabela:
grades
ID
songID
grade
ID - to klucz główny, songID to identyfikator piosenki z innej tabeli
Grade - to ocena, jaką dostała piosenka.
Teraz potrzebuję uzyskać dane odnośnie: ile dana piosenka otrzymała jedynek, ile dwójek, ...., ile szóstek.
Już kombinowałem ze wszystkim. Z case'ami, z joinami, z uniami(MysQl w ogóle ma unie?) i nic.
Jedyne, co mi się wydawało logiczne to to:
select
count(o.grade) as one,
count(t.grade) as two,
count(th.grade) as three,
count(f.grade) as four,
count(fi.grade) as five,
count(s.grade) as six
from grades o
left join grades t on t.grade = 2 and t.songID = 15
left join grades th on th.grade = 3 and th.songID = 15
left join grades f on f.grade = 4 and f.songID = 15
left join grades fi on fi.grade = 5 and fi.songID = 15
left join grades s on s.grade = 6 and s.songID = 15
where o.songID = 15 and o.grade = 1
Oczywiście nie działa, pokazuje jakieś w ogóle z kosmosu wzięte liczby(konkretnie którąś wielokrotność ogólnej ilości rekordów w tabeli) w każdej kolumnie te same.
Już nie mam pomysłów. Pomóżcie...