无法访问列pick_country_iso

时间:2015-02-02 08:37:26

标签: mysql sql database

我有这个查询

SELECT pick_up_id, (select pick_name from pick_up_point where pick_up_point.pick_up_id = s.pick_up_id limit 1) as pick_name, total_boxes, (SUM(r_occupied) + SUM(s_occupied)) AS total_occupied, (total_boxes - (SUM(r_occupied) + SUM(s_occupied))) as free_boxes, round(((SUM(r_occupied) + SUM(s_occupied)) / total_boxes * 100),2) as pct_usage,(select  pick_country_iso from pick_up_point where pick_up_point.pick_up_id = s.pick_up_id limit 1) as pick_country_iso
        FROM(
            SELECT p.r_pick_up_id AS pick_up_id, COUNT(p.parcel_id) AS r_occupied, 0 AS s_occupied,
            (SELECT COUNT(rl.lookup_id) FROM racks r INNER JOIN rack_lookup rl ON rl.rack_type = r.rack_type WHERE r.pick_up_id = p.r_pick_up_id) AS total_boxes
            FROM v_last_parcel_event p
            WHERE p.event_type IN (2,3,6,8,16,18,26,27,101,102,104,105,107,112,122,124,127,151,152,153)
            AND p.box_size <= 3 AND IFNULL(p.overflow_parcel, 0) = 0
            AND p.r_pick_up_id IS NOT NULL
            GROUP BY p.r_pick_up_id

            UNION ALL

            SELECT p.s_pick_up_id AS pick_up_id, 0 AS r_occupied, COUNT(p.parcel_id) AS s_occupied,
            (SELECT COUNT(rl.lookup_id) FROM racks r INNER JOIN rack_lookup rl ON rl.rack_type = r.rack_type WHERE r.pick_up_id = p.s_pick_up_id) AS total_boxes
            FROM v_last_parcel_event p
            WHERE p.event_type IN (2,6,8,12,16,18,26,27,101,102,104,105,107,112,122,124,151,152,153)
            AND p.box_size <= 3 AND IFNULL(p.overflow_parcel, 0) = 0
            AND p.s_pick_up_id IS NOT NULL
            GROUP BY p.s_pick_up_id
        ) as s 
        GROUP BY s.pick_up_id;

在第一行上面的查询中,我得到了列pick_country_iso

(select  pick_country_iso from pick_up_point where pick_up_point.pick_up_id = s.pick_up_id limit 1) as pick_country_iso

结果如下图所示。 enter image description here

现在我正在尝试在上面的查询中添加一个where子句

as s WHERE pick_country_iso="ABC"
        GROUP BY s.pick_up_id;

但是我收到了这个错误#1054 - 'where子句'中的未知列'pick_country_iso'。 谢谢你的帮助。

0 个答案:

没有答案