mysql/mariadb - LEFT JOIN aggregate not returning all values -
first up, don't know how word question if there's better terminology or phrasing, feel free edit.
so here's schema: http://sqlfiddle.com/#!9/ca46c1/2
create table map ( id int unsigned primary key not null auto_increment ); create table vote_map ( id int unsigned primary key not null auto_increment, user_id int unsigned not null, map_id int unsigned not null, score enum("-1", "0", "1") ); create view view_vote_map_rank select map.id map_id, coalesce( sum(cast(cast(score char) signed)), 0) score vote_map right join map on map.id = vote_map.map_id group map_id; insert map (id) values (1); insert map (id) values (2); insert map (id) values (3); insert map (id) values (4); insert map (id) values (5); insert vote_map (user_id, map_id, score) values (1, 1, '1'); insert vote_map (user_id, map_id, score) values (2, 2, '1'); select * map; select * view_vote_map_rank;
the results i'm getting are
map_id score 3 0 1 1 2 1
however incomplete. expecting id 4
, 5
there well, score of 0
too. i'm not sure why it's stopping after first 0
. missing?
in view, use following:
select a.id, sum(if(b.score null,0,b.score)) `score` map left join vote_map b on a.id = b.map_id group a.id
simpler , in sql fiddle, seems return correct results
Comments
Post a Comment