簡體   English   中英

左外連接和計數不會列出所有行

[英]Left outer join and count does not list all the rows

$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還是更大。 我的問題是什么?

與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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM