mysql - Strange order of results when adding joins -


i'm trying build commenting system on website having issues ordering comments correctly. screenshot of had before went wrong:

enter image description here

and query before went wrong:

select     com.comment_id,     com.parent_id,     com.is_reply,     com.user_id,     com.comment,     com.posted,     usr.username     blog_comments com left join     users usr on com.user_id = usr.user_id     com.article_id = :article_id , com.moderated = 1 , com.status = 1 order     com.parent_id desc; 

i want include each comment's votes blog_comment_votes table, using left outer join, , came query, works, screws order of results:

select     com.comment_id,     com.parent_id,     com.is_reply,     com.user_id,     com.comment,     com.posted,     usr.username,     ifnull(c.cnt,0) votes     blog_comments com left join     users usr on com.user_id = usr.user_id left outer join (     select comment_id, count(vote_id) cnt     blog_comment_votes     group comment_id) c     on com.comment_id = c.comment_id     com.article_id = :article_id , com.moderated = 1 , com.status = 1 order     com.parent_id desc; 

i order, bizarre:

enter image description here

i tried adding group clause on com.comment_id failed too. can't understand how adding simple join can alter order of results! can on correct path?

example table data , expected results

these relevant tables example data:

[users]

user_id | username --------|----------------- 1       | paparazzokid 

[blog_comments]

comment_id | parent_id | is_reply | article_id | user_id |  comment       -----------|-----------|----------|------------|---------|--------------------------- 1          | 1         |          | 1          | 1       |  first comment 2          | 2         | 1        | 1          | 20      |  reply first comment 3          | 3         |          | 1          | 391     |  second comment 

[blog_comment_votes]

vote_id | comment_id | article_id | user_id --------|------------|------------|-------------- 1       | 2          | 1          | 233 2       | 2          | 1          | 122 

so order should be

first comment     reply first comment    +2 second comment 

it's difficult without looking @ query results, guess it's because ordering parent id , not saying how order when 2 records have same parent id. try changing query this:

select     com.comment_id,     com.parent_id,     com.is_reply,     com.user_id,     com.comment,     com.posted,     usr.username,     count(c.votes) votes     blog_comments com left join     users usr on com.user_id = usr.user_id left join      blog_comment_votes c on com.comment_id = c.comment_id     com.article_id = :article_id , com.moderated = 1 , com.status = 1 group      com.comment_id,     com.parent_id,     com.is_reply,     com.user_id,     com.comment,     com.posted,     usr.username order     com.parent_id desc, com.comment_id; 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -