我有這個tbl_religion與字段名creldesc顯示值不存在於另一個表
和TBL_MEMBER這些領域
,我有這個疑問算在TBL_MEMBER所有現存宗教和也算不算。男性和女性與宗教的所以我做了這個查詢:
SELECT m.creldesc as type,
COUNT(m.creldesc) as total,
SUM(CASE WHEN m.cgender='Male' THEN 1 ELSE 0 END) as male,
SUM(CASE WHEN m.cgender='Female' THEN 1 ELSE 0 END) as female
FROM tbl_member as m, tbl_barangay as b, tbl_household as h
WHERE m.chholdnumber = h.chholdnumber and h.cbrgycode=b.cbrgycode and b.cbrgyname = 'AGAO'
and m.crelationdesc !='Brgy. Captain'
GROUP BY m.creldesc
ORDER BY tot DESC;
,並得到如下的輸出:
我想要實現的是也以0作爲顯示來自tbl_religion剩餘的宗教值。我做了一個左連接但出現錯誤。有人可以幫助我如何達到目的?
type total male female
Roman Catholic 7 4 3
Baptist 3 1 2
Islam 3 3 0
Iglesia ni Cristo 1 1 0
Free Methodist 1 1 0
Ang Dating Daan 1 0 1
Aglipay/Filipinsta 0 0 0
All the Gospel 0 0 0
Alpha Omega 0 0 0
and so on.............................
什麼樣的錯誤? – SanRyu 2014-09-19 07:20:09
使用'LEFT JOIN' – Wanderer 2014-09-19 07:21:06
SELECT \t m.creldesc類型, \t COUNT(m.creldesc)作爲TOT, \t SUM(CASE WHEN m.cgender = '男' THEN ELSE 1 0 END)爲男性, \t SUM(CASE WHEN m.cgender = '女' THEN ELSE 1 0 END)如女性 \t FROM TBL_MEMBER表爲m,tbl_barangay爲b,tbl_household爲h \t左連接tbl_religion ON tbl_religion.creldesc = m.creldesc \t WHERE m.chholdnumber = h.chholdnumber和h.cbrgycode = b.cbrgycode和b.cbrgyname ='AGAO'和m.crelationdesc!='Brgy。隊長' \t GROUP BY m.creldesc \t ORDER BY tot DESC – ecoli 2014-09-19 07:22:25