MySQL:未知列'%'在' order子句'错误

时间:2016-02-25 21:32:02

标签: mysql sql-order-by having union-all sequelpro

以下(未删节)MySQL查询:

/* May Report */
SELECT db.prod.id AS "Prod. Spec. ID (TEST)",
    db.prod.cycle_type AS "BiCode (TEST)",
    SUM(db.prod.cycles) "Number of Cycles (TEST)",
    db.instetail_d.id AS "Detail # (TEST)",
    db.cont.id AS "Unique ID (TEST)",
    db.sale.id AS "Non-U. ID (TEST)",
    DATE_FORMAT(db.prod.start_date,'%m-%d-%Y') AS "START DATE (TEST)",
    DATE_FORMAT(db.prod.end_date,'%m-%d-%Y') AS "END DATE (TEST)",
    db.sale.name AS "ESTIMATE NAME",
    db.pub_l.pub_name AS "PUBLICATION",
    db.pub_l.s AS "SECTION",
    CASE WHEN db.prod.market_code = "NNJ" THEN "NORTHERN NJ"
        WHEN db.prod.market_code = "HAT CT" THEN "HARTFORD-NEW HAVEN"
        ELSE NULL END AS "Market",
    db.prod.market_code AS "MARKET CODE (TEST)",
    db.instetail_d.market AS "MARKET (TEST)",
    CONCAT_WS(" - ", db.prod.format, DATE_FORMAT(db.prod.start_date,'%m/%d/%Y'), DATE_FORMAT(db.prod.end_date,'%m/%d/%Y')) AS "VEHICLE/QTY",
    db.temp.brand_name AS "PRODUCT",
    db.prod.product AS "PROD CODE",
    db.prod.sig AS "LOC DESCRIPTION",
    db.instetail_d.subset AS "EMP NAME",
    db.rd.target_audience AS "PACKAGE TARGET",
    (db.prod.cos * 0.37) AS "COSTS (NET)",
    (db.prod.comment5 / 4) AS "dynamicColumnHeader"
    FROM db.prod
    INNER JOIN temp
        ON temp.product_code = prod.product
    INNER JOIN db.cont_prod
        ON db.prod.id = db.cont_prod.prod
    INNER JOIN db.cont
        ON db.cont_prod.cont = db.cont.id
    INNER JOIN db.pub_l
        ON (db.cont.buying_company_id = db.pub_l.buying_company_id AND db.cont.selling_company_id = db.pub_l.selling_company_id )
    INNER JOIN db.sale
        ON db.cont.sale = db.sale.id
    INNER JOIN db.inst
        ON (db.cont.id = db.inst.cont AND db.sale.id = db.inst.sale)
    INNER JOIN db.instetails_d
        ON db.inst.id = db.instetails_d.inst
    INNER JOIN db.instetail_d
        ON db.instetails_d.detail = db.instetail_d.id
    LEFT JOIN db.rd
        ON db.cont.rfp_id = db.rd.id
    WHERE (db.sale.id = 1818 OR db.sale.id = 1857)
    AND db.prod.uni_hash_target = db.instetail_d.uni_hash_target
    AND (db.instetail_d.location_description = db.prod.sig OR db.instetail_d.location_description IS NULL OR db.instetail_d.location_description = "")
    AND db.prod.space NOT IN ("PRODUCTION","TAXES","CREDIT","INSTALLCHRG","MAINTENANCE","EXTCHARGE")
    AND (DATE_FORMAT(db.prod.start_date, "%m") = 5 OR (DATE_FORMAT(db.prod.start_date, "%m") = 6) AND DATE_FORMAT(db.prod.end_date, "%d") > 4)
    GROUP BY db.prod.sig
    /* ORDER BY DATE_FORMAT(db.prod.start_date, "%m") ASC, DATE_FORMAT(db.prod.start_date, "%d") ASC; */
UNION ALL
SELECT db.prod.id AS "Prod. Spec. ID (TEST)",
    db.prod.cycle_type AS "BiCode (TEST)",
    SUM(db.prod.cycles) "Number of Cycles (TEST)",
    db.instetail_d.id AS "Detail # (TEST)",
    db.cont.id AS "Unique ID (TEST)",
    db.sale.id AS "Non-U. ID (TEST)",
    DATE_FORMAT(db.prod.start_date,'%m-%d-%Y') AS "START DATE (TEST)",
    DATE_FORMAT(db.prod.end_date,'%m-%d-%Y') AS "END DATE (TEST)",
    db.sale.name AS "ESTIMATE NAME",
    db.pub_l.pub_name AS "PUBLICATION",
    db.pub_l.s AS "SECTION",
    CASE WHEN db.prod.market_code = "NNJ" THEN "NORTHERN NJ"
        WHEN db.prod.market_code = "HAT CT" THEN "HARTFORD-NEW HAVEN"
        ELSE NULL END AS "Market",
    db.prod.market_code AS "MARKET CODE (TEST)",
    db.instetail_d.market AS "MARKET (TEST)",
    CONCAT_WS(" - ", db.prod.format, DATE_FORMAT(db.prod.start_date,'%m/%d/%Y'), DATE_FORMAT(db.prod.end_date,'%m/%d/%Y')) AS "VEHICLE/QTY",
    db.temp.brand_name AS "PRODUCT",
    db.prod.product AS "PROD CODE",
    db.prod.sig AS "LOC DESCRIPTION",
    db.instetail_d.subset AS "EMP NAME",
    db.rd.target_audience AS "PACKAGE TARGET",
    (db.prod.cos * 0.37) AS "COSTS (NET)",
    (db.prod.comment5 / 4) AS "dynamicColumnHeader"
    FROM db.prod
    INNER JOIN temp
        ON temp.product_code = prod.product
    INNER JOIN db.cont_prod
        ON db.prod.id = db.cont_prod.prod
    INNER JOIN db.cont
        ON db.cont_prod.cont = db.cont.id
    INNER JOIN db.pub_l
        ON (db.cont.buying_company_id = db.pub_l.buying_company_id AND db.cont.selling_company_id = db.pub_l.selling_company_id )
    INNER JOIN db.sale
        ON db.cont.sale = db.sale.id
    INNER JOIN db.inst
        ON (db.cont.id = db.inst.cont AND db.sale.id = db.inst.sale)
    INNER JOIN db.instetails_d
        ON db.inst.id = db.instetails_d.inst
    INNER JOIN db.instetail_d
        ON db.instetails_d.detail = db.instetail_d.id
    LEFT JOIN db.rd
        ON db.cont.rfp_id = db.rd.id
    WHERE (db.sale.id = 1818 OR db.sale.id = 1857)
    AND db.prod.uni_hash_target = db.instetail_d.uni_hash_target
    AND (db.instetail_d.location_description = db.prod.sig OR db.instetail_d.location_description IS NULL OR db.instetail_d.location_description = "")
    AND db.prod.space NOT IN ("PRODUCTION","TAXES","CREDIT","INSTALLCHRG","MAINTENANCE","EXTCHARGE")
    AND (DATE_FORMAT(db.prod.start_date, "%m") = 5 OR (DATE_FORMAT(db.prod.start_date, "%m") = 6) AND DATE_FORMAT(db.prod.end_date, "%d") > 4)
    GROUP BY db.prod.sig
    HAVING SUM(db.prod.cycles) >= 0.5
    ORDER BY DATE_FORMAT(db.prod.start_date, "%m") ASC, DATE_FORMAT(db.prod.start_date, "%d") ASC;

产生以下错误:

Unknown column 'Unknown column 'db.prod.start_date' in 'order clause'

使用Sequel Pro。

我的目标是复制符合以下条件的每一列:

SUM(db.prod.cycles) >= 0.5

整个查询将在给我错误之前完成(大约一分钟左右)。

我尝试创建子查询并使用UNION代替UNION ALL来解决问题而不会成功。

1 个答案:

答案 0 :(得分:1)

两个查询在UNION之后,字段“db.prod.start_date”现在称为“START DATE(TEST)”。尝试更改ORDER BY子句以引用新的“作为列名。”

示例:ORDER BY START DATE (TEST)