带有子查询的一个查询中的HAVING子句

时间:2013-04-08 04:48:02

标签: php mysql sql 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

我想要“HAVING ng_sponsored&lt;&gt; 0”

谢谢!

2 个答案:

答案 0 :(得分: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

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

  • FROM clause
  • WHERE子句
  • GROUP BY子句
  • HAVING条款
  • SELECT条款
  • ORDER BY子句

答案 1 :(得分:-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