Простое соединение MySQL не работает - PullRequest
1 голос
/ 02 декабря 2011

Я пытаюсь выбрать комментарии и сводную статистику об их соответствующих голосах.Не у каждого комментария есть голоса, и у тех, у которых есть, может быть больше чем один голос.Вот мой запрос:

select
    `article-comments`.id,
    `article-comments`.user as user_id,
    concat(users.first_name, ' ', users.last_name) as name,
    users.job_title,
    users.company_name,
    avatar,
    `article-comments`.content,
    datediff(now(), `article-comments`.date_added) as diff,
    `article-comments`.date_added as date,
    count(`comment-votes`.id) as votes_count,
    sum(`comment-votes`.score) as votes_score
from
    `article-comments` left outer join `comment-votes` on `article-comments`.id = `comment-votes`.comment,
    users
where
    `article-comments`.status = 1
 && `article-comments`.article = $resource_id
 && `article-comments`.user = users.id
 && `comment-votes`.status = 1
order by
    `article-comments`.id asc

Он отлично работает без объединения, но возвращает только 1 строку с ним.

Любые идеи?

1 Ответ

0 голосов
/ 02 декабря 2011

Попробуйте:

select
                `article-comments`.id,
                `article-comments`.user as user_id,
                concat(users.first_name, ' ', users.last_name) as name,
                users.job_title,
                users.company_name,
                avatar,
                `article-comments`.content,
                datediff(now(), `article-comments`.date_added) as diff,
                `article-comments`.date_added as date,
                count(`comment-votes`.id) as votes_count,
                sum(`comment-votes`.score) as votes_score
            from
                `article-comments` left join `comment-votes` on (`article-comments`.id = `comment-votes`.comment),
                users
            where
                `article-comments`.status = 1
             && `article-comments`.$table = $resource_id
             && `article-comments`.user = users.id
            order by
                `article-comments`.id asc
...