为什么订单不在我的Union查询中?

时间:2014-04-03 10:01:35

标签: mysql sql sql-order-by union

我试图通过在3个表之间进行此查询来按月订购:

SELECT NULL AS `inState`, NULL AS `outState`, mb.`isDuplicate`, mb.`questStatus`,   mb.state, mb.`subState`, mb.`recomputedOn`, c.`TSsubmitOn`, c.`submittedOn`, mb.week, mb.month 
FROM metric_backlog mb INNER JOIN `CR` c ON c.crdbid = mb.crdbid 
WHERE (mb.`productName` = 'ecc' AND mb.`releaseName` 
IN ('6.7.3', '6.5.0', '6.7.0', '6.7.1', '6.6.0', '6.7.2', '6.2.0', '6.1.0')) AND mb.month = '1101'

UNION ALL 
SELECT mi.`inState`, mi.`outState`, NULL AS sq, NULL AS ee, NULL AS yy, NULL AS qq,   NULL AS xx, NULL AS mer, NULL AS yi, mi.week, mi.month as monthh
FROM metric_inout mi INNER JOIN `CR` c ON c.crdbid = mi.crdbid 
WHERE mi.month = '1101' AND mi.month != "NULL" AND mi.month IS NOT NULL AND 
mi.`productName` = 'ecc' AND mi.`releaseName` 
IN ('6.7.3', '6.5.0', '6.7.0', '6.7.1', '6.6.0', '6.7.2', '6.2.0', '6.1.0')
ORDER BY mi.month

我收到错误:订单条款中的未知列mi.month

谢谢!

2 个答案:

答案 0 :(得分:0)

按顺序尝试:

  ORDER BY month

而不是从第二次查询中选择月份

  SELECT NULL AS `inState`, NULL AS `outState`, mb.`isDuplicate`, mb.`questStatus`,   mb.state, mb.`subState`, mb.`recomputedOn`, c.`TSsubmitOn`, c.`submittedOn`, mb.week, mb.month as month

答案 1 :(得分:0)

使用UNION ALL,您可以选择具有从第一个select语句收集的名称的列。没有与这些名称相关联的langer表名称。因此,您选择:inState,outState,isDuplicate,questStatus,state,subState,recomputedOn,TSsubmitOn,submittedOn,week和month。 (顺便说一句,来自第二个的名称 - 以及更多的SQL语句 - 如果有的话 - 完全不相关。)

因此,你无法通过mi.month订购。应用UNION ALL后,表关联字段“mi.month”不再可用。只有工会字段“月”。您只能按月订购。