我在MySQL视图查询中收到错误,

时间:2017-10-06 11:01:54

标签: mysql sql mysql-error-1054

    SELECT DISTINCT `co`.`CustomerID` AS `CustomerID`,
        `c`.`CustomerName` AS `CustomerName`,
        `c`.`CustomerAcronym` AS `CustomerAcronym`,
        `co`.`CustomerOrderID` AS `CustomerOrderID`,
        `co`.`CustomerOrderNumber` AS `CustomerOrderNumber`,
        `co`.`ProductionStatusID` AS `ProductionStatusID`,
        `ps`.`Name` AS `ProductionStatusName`,
        `sm`.`Name` AS `ShippingMethod`,
        `ois`.`TrackingNumber` AS `TrackingNumber`,
        `co`.`OrderSubmittedDate` AS `ReceivedDate`,
        `co`.`ExpectedShippingDate` AS `ExpectedShippingDate`,
        MAX(`ois`.`ShippingDate`) AS `ActualShippingDate`,
        (SELECT `CustomerOrderID` from `pt2_prod`.`orderitem` `io_one` where `io_one`.`PhotoTrackID` = "LMN_R000255346-101") as 'CustomerOrderIDOrderItem'
FROM `pt2_prod`.`customerorder` `co`
JOIN `pt2_prod`.`customer` `c` ON((`co`.`CustomerID` = `c`.`CustomerID`))
JOIN `pt2_prod`.`orderitem` `oi` ON `oi`.`CustomerOrderID` = `co`.`CustomerOrderID`
LEFT JOIN `pt2_prod`.`orderitemshipping` `ois` ON `ois`.`OrderItemID` = `oi`.`OrderItemID`
JOIN `pt2_prod`.`shippingmethod` `sm` ON `sm`.`ShippingMethodID` = `co`.`ShippingMethodID`
JOIN `pt2_prod`.`productionstatus` `ps` ON `co`.`ProductionStatusID` = `ps`.`ProductionStatusID`
where 'CustomerOrderIDOrderItem' = `co`.`CustomerOrderID`
GROUP BY `c`.`CustomerName`,
         `c`.`CustomerAcronym`,
         `co`.`CustomerOrderNumber`,
         `sm`.`Name`,
         `co`.`ExpectedShippingDate`,
         `ois`.`TrackingNumber`,
         `co`.`OrderSubmittedDate`,
         `ps`.`Name`

以上是我的MySQL视图查询。我也得到了同样的错误。 我的MySQL版本是XAMPP中的10.1.16-MariaDB。请帮我解决这个错误。谢谢。

MySQL错误:#1054 - 未知列' CustomerOrderIDOrderItem'在' where子句'

3 个答案:

答案 0 :(得分:1)

如果您像以前那样派生了+别名列,则无法使用WHERE关键字。您必须使用HAVING关键字。

您的查询不会是:

where `CustomerOrderIDOrderItem` = `co`.`CustomerOrderID`

它应该是:

HAVING `CustomerOrderIDOrderItem` = `co`.`CustomerOrderID`

答案 1 :(得分:0)

否则只需添加cross join,例如:

SELECT DISTINCT `co`.`CustomerID` AS `CustomerID`,
        `c`.`CustomerName` AS `CustomerName`,
        `c`.`CustomerAcronym` AS `CustomerAcronym`,
        `co`.`CustomerOrderID` AS `CustomerOrderID`,
        `co`.`CustomerOrderNumber` AS `CustomerOrderNumber`,
        `co`.`ProductionStatusID` AS `ProductionStatusID`,
        `ps`.`Name` AS `ProductionStatusName`,
        `sm`.`Name` AS `ShippingMethod`,
        `ois`.`TrackingNumber` AS `TrackingNumber`,
        `co`.`OrderSubmittedDate` AS `ReceivedDate`,
        `co`.`ExpectedShippingDate` AS `ExpectedShippingDate`,
        MAX(`ois`.`ShippingDate`) AS `ActualShippingDate`,
        `CustomerOrderIDOrderItem`. `CustomerOrderID` AS 'CustomerOrderIDOrderItem'
FROM `pt2_prod`.`customerorder` `co`
JOIN `pt2_prod`.`customer` `c` ON((`co`.`CustomerID` = `c`.`CustomerID`))
JOIN `pt2_prod`.`orderitem` `oi` ON `oi`.`CustomerOrderID` = `co`.`CustomerOrderID`
LEFT JOIN `pt2_prod`.`orderitemshipping` `ois` ON `ois`.`OrderItemID` = `oi`.`OrderItemID`
JOIN `pt2_prod`.`shippingmethod` `sm` ON `sm`.`ShippingMethodID` = `co`.`ShippingMethodID`
JOIN `pt2_prod`.`productionstatus` `ps` ON `co`.`ProductionStatusID` = `ps`.`ProductionStatusID`
CROSS JOIN (SELECT `CustomerOrderID` from `pt2_prod`.`orderitem` `io_one` where `io_one`.`PhotoTrackID` = "LMN_R000255346-101") as CustomerOrderIDOrderItem
where `CustomerOrderIDOrderItem`.`CustomerOrderID` = `co`.`CustomerOrderID`
GROUP BY `c`.`CustomerName`,
         `c`.`CustomerAcronym`,
         `co`.`CustomerOrderNumber`,
         `sm`.`Name`,
         `co`.`ExpectedShippingDate`,
         `ois`.`TrackingNumber`,
         `co`.`OrderSubmittedDate`,
         `ps`.`Name`,
         `CustomerOrderIDOrderItem`. `CustomerOrderID`

答案 2 :(得分:-2)

  

MySQL错误:#1054 - 'where子句'中的未知列'CustomerOrderIDOrderItem'

试,

where `co`.`CustomerOrderID` = `CustomerOrderIDOrderItem`

代替,

where  `CustomerOrderIDOrderItem` = `co`.`CustomerOrderID`