MySQL:“顺序子句”错误中的未知列“%”

Posted

技术标签:

【中文标题】MySQL:“顺序子句”错误中的未知列“%”【英文标题】:MySQL: Unknown column '%' in 'order clause' Error 【发布时间】:2016-02-25 21:32:02 【问题描述】:

以下(未删节的)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】:

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

示例:ORDER BY START DATE (TEST)

【讨论】:

查询有效,但现在两个表都是ORDER BY 不是在一个完整的表中,而是在两个表中,一个在另一个之后。例如,我想让我的报告的第一个条目翻倍。它确实翻了一番,但第二行可以在UNION ALL 中第一个表的最后一行之后找到。我该如何解决这个问题? 一种快速而肮脏的方法是在两个查询中添加一列并首先按该列排序,然后按其余列排序。第一个查询中的列可能类似于“1 as QueryNumber”。在第二个查询“2 as QueryNumber”中。按查询编号等排序... 又工作了!我只是使用sort 作为列标题,但它有效!非常感谢!

以上是关于MySQL:“顺序子句”错误中的未知列“%”的主要内容,如果未能解决你的问题,请参考以下文章

#1054 - MySQL 中“where 子句”中的未知列“proximite”

有一个 mySQL 错误,未知列 where 子句 [重复]

简单选择语句的where子句中的MySQL未知列

MySQL错误1054未知列'persons.PersonID' in on子句[重复]

MySQL“On 子句中的未知列”[重复]

错误代码:1054 'where 子句'中的未知列'billId'