0

是否可以在一个查询中有多个 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_support <> 0”

谢谢!

4

2 回答 2

0

您可以计算count满足子查询中条件的每个。这里需要的过滤运算符只是WHERE而不是HAVING因为您在外部查询上没有进一步的聚合。

SELECT  a.household_connector_id AS h_id,
        COALESCE(b.totalCount, 0) + COALESCE(c.totalCount, 0) AS total_phic,
        COALESCE(b.totalCountSponsored, 0) + COALESCE(c.totalCountSponsored, 0) AS ng_sponsored
FROM    household_tbl a 
        INNER JOIN
        (
            SELECT  household_connector_id, 
                    COUNT(*) totalCount,
                    SUM(hh_phic_membership_category IN ('ng sponsored', 'Sponsored-NG(DOH)')) totalCountSponsored
            FROM    household_tbl
            WHERE   hh_phic_status = 1 AND 
                    age >= 21 AND 
                    age_category =  "Year"
            GROUP   BY household_connector_id           
        ) b ON a.household_connector_id = b.household_connector_id
        LEFT JOIN
        (
            SELECT  household_connector_id, 
                    COUNT(*) totalCount,
                    SUM(hh_phic_membership_category IN ('ng sponsored', 'Sponsored-NG(DOH)')) totalCountSponsored
            FROM    tbl_household_members 
            WHERE   hh_phic_status = 1 AND 
                    hh_age >= 21 AND 
                    hh_age_category = "Year"
            GROUP   BY household_connector_id
        ) c ON a.household_connector_id = c.household_connector_id
WHERE   a.barangay = "barangay_value_HERE" AND
        COALESCE(b.totalCount, 0) + COALESCE(c.totalCount, 0) <> 0 AND
        COALESCE(b.totalCountSponsored, 0) + COALESCE(c.totalCountSponsored, 0) <> 0

您也不能使用在同一级别上创建的ALIASon子句,WHERE因为ALIASwere created onSELECT子句和WHERE子句在之前执行SELECT。这是完整的 SQL 操作顺序,

  • FROM 子句
  • WHERE 子句
  • GROUP BY 子句
  • HAVING 子句
  • 选择子句
  • ORDER BY 子句
于 2013-04-08T05:18:46.717 回答
-1

做这个:

            Select * From
            (
                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.'"
                )tbl Group By h_id,total_phic,ng_sponsored 
                HAVING total_phic <> 0

您可以更换:

Group By h_id,total_phic,ng_sponsored HAVING total_phic <> 0

经过

Where total_phic <> 0
于 2013-04-08T04:51:37.810 回答