0
$select = "select  f.root_id,f.dateTime,f.name,count(fs.id) as sub_count,CONCAT(u.name,\" \",u.surname) as creator_name_surname".
            " from forum as f ".
            " left outer join users as u on u.id=f.creator_id".
            " left outer join forum as fs on fs.record_root_id=f.root_id"
            ;
    $params = " where fs.status=1 and f.status = 1 and f.record_root_id=$root_id and f.kullanici_firma_id=$kullanici_firma_id";

    $group_by =" GROUP BY f.root_id,f.dateTime,f.name";

此查询不列出 f 中的行count(fs.id)=0。但我需要列出所有其他行,无论计数为 0 还是更大。我的问题是什么?

4

1 回答 1

2

WHERE 过滤器的问题fs.status=1。这导致forum fs表上的 LEFT JOIN 就像 INNER JOIN 一样。您应该将该过滤器放在 JOIN 上。:

select  f.root_id,
    f.dateTime,
    f.name,
    count(fs.id) as sub_count,
    CONCAT(u.name,\" \",u.surname) as creator_name_surname
from forum as f 
left outer join users as u 
    on u.id=f.creator_id
left outer join forum as fs 
    on fs.record_root_id=f.root_id
    and fs.status=1 
where f.status = 1 
    and f.record_root_id=$root_id 
    and f.kullanici_firma_id=$kullanici_firma_id
GROUP BY f.root_id,f.dateTime,f.name
于 2013-05-28T21:03:55.087 回答