MySQL left join excludes non-matching records? -


when execute mysql query, correct results expected:

select h.id productid, h.producttitle, u.username, ht.producttype, hd.difficulty-- , ifnull(sum(pv.upvote),0) - ifnull(sum(pv.downvote),0) netvotes products h  join users u on h.userid = u.id  join producttypes ht on h.producttypeid = ht.id  join productdifficulties hd  on h.productdifficultyid = hd.id  -- left join productvotes pv -- on h.id = pv.productid 

notice commented out last column in select (aggregate) , left join. when include in sql, records have matching productvotes record included:

select h.id productid, h.producttitle, u.username, ht.producttype, hd.difficulty, ifnull(sum(pv.upvote),0) - ifnull(sum(pv.downvote),0) netvotes products h  join users u on h.userid = u.id  join producttypes ht on h.producttypeid = ht.id  join productdifficulties hd  on h.productdifficultyid = hd.id  left join productvotes pv on h.id = pv.productid 

i expect behavior inner join, not left join. why left join excluding records without matching productvotes record? , how can have included, in first query above?

edit: had limit 0, 10 @ end of query, , folks thought causing problem. removed limit clause , problem still remains, had nothing it. have removed limit clause original post because it's irrelevant. still awaiting answer. :-)

here full results of select * productvotes:

id productid userid upvote downvote  2         1      2      1 null  3         1      3      1 null  4         1      4      1 null  5         1      5      1 null  6         1      6      1 null  7         1      7      1 null  8         1      8      1 null  9         1      9   null    1 10         1     10   null    1 22         1      1      1 null 

(notice 10 records in table.)

important: please notice aggregations in select statements: ifnull(sum(pv.upvote),0) - ifnull(sum(pv.downvote),0) netvotes

select h.id productid, h.producttitle, u.username, ht.producttype, hd.difficulty,  (select ifnull(sum(pv.upvote),0) - ifnull(sum(pv.downvote),0) productvotes pv h.id = pv.productid ) netvotes products h  join users u on h.userid = u.id  join producttypes ht on h.producttypeid = ht.id  join productdifficulties hd  on h.productdifficultyid = hd.id 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -