在一個查詢中是否可以有多個HAVING?HAVING子句在子查詢中的查詢
這裏是我的示例查詢:
SELECT household_tbl.household_connector_id AS h_id
, (
SELECT COUNT(household_connector_id)
FROM household_tbl
WHERE household_connector_id = h_id
AND hh_phic_status = 1
AND age >= 21
AND age_category = "Year"
) + (
SELECT COUNT(hh_members_connector_id)
FROM tbl_household_members
WHERE household_connector_id = h_id
AND hh_phic_status = 1
AND hh_age >= 21
AND hh_age_category = "Year"
) AS total_phic
, (
SELECT COUNT(household_connector_id)
FROM household_tbl
WHERE household_connector_id = h_id
AND hh_phic_status = 1
AND age >= 21
AND age_category = "Year"
AND (
hh_phic_membership_category = "ng sponsored"
OR hh_phic_membership_category = "Sponsored-NG(DOH)"
)
) + (
SELECT COUNT(hh_members_connector_id)
FROM tbl_household_members
WHERE household_connector_id = h_id
AND hh_phic_status = 1
AND hh_age >= 21
AND hh_age_category = "Year"
AND (
hh_phic_membership_category = "ng sponsored"
OR hh_phic_membership_category = "Sponsored-NG(DOH)"
)
) AS ng_sponsored
FROM household_tbl
WHERE barangay = "'.$barangay.'"
HAVING total_phic <> 0
我想也有 「具有ng_sponsored <> 0」
謝謝!
怎麼樣增加'where子句'而不是'HAVING'? – TechDo 2013-04-08 04:51:09
試試這個有total_phic <> 0和ng_sponsored <> 0 – 2013-04-08 04:53:29
hello kabayan':)'你能指定表結構並告訴我們你想做什麼?無論如何恭喜你畢業.. – 2013-04-08 04:53:41