1
我有以下語句,它工作正常,除非事實上,如果第二個表沒有引用項目的第一個表,它將返回沒有結果。SQL查詢返回0而不是NULL
我需要的是它返回count(p.match_id) AS matchcount
爲0
"SELECT c.*, count(p.match_id) AS matchcount
FROM ci_address_book c LEFT JOIN ci_matched_sanctions p
ON c.id = p.addressbook_id
GROUP BY p.addressbook_id ORDER BY c.id LIMIT ".$offset.",".$num;
部分爲真...我的聲明{「SELECT c。*,COALESCE(COUNT(p.match_id),0)AS matchcount FROM ci_address_book c LEFT OUTER JOIN ci_matched_sanctions p ON c.id = p.addressbook_id GROUP BY p.addressbook_id ORDER BY c.id LIMIT「。$ offset。」,「。$ num;} ...這將只給我回數量的結果就像我在matching_sanctions表中一樣。我的通訊錄現在有80個名字。如果matched_sanctions根本沒有行,我什麼也得不到。如果它有1,我得到1,如果它有2我得到2等... – renevdkooi 2010-11-17 03:59:47
也許換句話說我的q。我有一個名稱(地址簿)的表,我有一個表,其中包含匹配。由id引用。所以它是一個多關係。我想計算匹配表中的行數,同時獲取地址簿的所有結果。所以我想看到:addressbook_id:1 - name - count – renevdkooi 2010-11-17 04:02:00
SELECT c。*,IFNULL(p.total,0)num FROM ci_address_book c LEFT JOIN(SELECT addressbook_id,COUNT(*)AS total FROM ci_matched_sanctions GROUP BY addressbook_id) AS p ON c.id = p.addressbook_id做了訣竅 – renevdkooi 2010-11-17 04:30:15