简单的MySQL连接不起作用

时间:2011-12-02 04:01:15

标签: mysql join outer-join

我正在尝试选择有关其相应投票的评论和摘要统计信息。不是每个评论都有投票,而且那些投票可以有超过1票。这是我的疑问:

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 :(得分:0)

尝试:

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