【问题标题】:What is Error#1221 in Mysql and is basic problem in this SqlQuery?Mysql 中的 Error#1221 是什么,这个 SqlQuery 的基本问题是什么?
【发布时间】:2009-09-17 12:51:02
【问题描述】:

我正在使用 MYSQL5.1,虽然我试图为此查找文档但没有成功,其次我想知道以下查询中的逻辑错误..

SQL 查询

选择日期、月份、总和(fact_1)、 ( 2 / Sum(fact_2) ) , 2 FROM( 选择 时间.日期,时间.月,时间.年, MAX(sales_fact.sell_out_value) 作为 fact_1, 0 as fact_2 FROM sales_fact, time_dim 作为时间 WHERE time.id=sales_fact.time_id AND time.date="2008-01-01" 分组 time.date ORDER BY time.year UNION 选择时间.日期,时间.月, time.year, 0 as fact_1, MAX(sales_target_fact.sell_out_target) 作为来自 sales_target_fact 的 fact_2, time_dim 作为时间 WHERE time.id=sales_target_fact.time_id AND time.date="2008-01-01" 分组 time.date ORDER BY time.year ) as Combined_Table GROUP BY 日期 ORDER BY 年份

错误行

  • UNION 和 ORDER BY 用法不正确
  • 错误代码#1221

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    该错误表明您的ORDER BY 不明确,您需要将您的SELECT 语句括起来以向MySQL 表明它是如何应用的。

    来自MySQL Docs

    使用 ORDER BY 或 LIMIT 子句 排序或限制整个 UNION 结果, 将单个 SELECT 括起来 声明并放置 ORDER BY 或 LIMIT 在最后一个之后。

    因此,根据您的查询,将每个查询放在括号内,并将 ORDER BY 放在括号外:

    SELECT date , month , Sum(fact_1) , ( 2 / Sum(fact_2) ) , 2 
    FROM( 
      (SELECT time.date, time.month, time.year, 
         MAX(sales_fact.sell_out_value) as fact_1, 0 as fact_2 
      FROM sales_fact, time_dim as time 
      WHERE time.id=sales_fact.time_id AND time.date="2008-01-01"
      GROUP BY time.date)
      UNION 
      (SELECT time.date, time.month, time.year, 0 as fact_1,
         MAX(sales_target_fact.sell_out_target) as fact_2
       FROM sales_target_fact, time_dim as time
       WHERE time.id=sales_target_fact.time_id AND time.date="2008-01-01"
       GROUP BY time.date)
       ORDER BY time.year
    ) as Combined_Table GROUP BY date ORDER BY year
    

    或者,如果您希望ORDER BYUNION 之前单独应用于每个语句,请将ORDER BY 放在两个SELECT 语句中的每一个的括号内。

    SELECT date , month , Sum(fact_1) , ( 2 / Sum(fact_2) ) , 2 
    FROM( 
      (SELECT time.date, time.month, time.year, 
         MAX(sales_fact.sell_out_value) as fact_1, 0 as fact_2 
      FROM sales_fact, time_dim as time 
      WHERE time.id=sales_fact.time_id AND time.date="2008-01-01"
      GROUP BY time.date
      ORDER BY time.year)
      UNION 
      (SELECT time.date, time.month, time.year, 0 as fact_1,
         MAX(sales_target_fact.sell_out_target) as fact_2
       FROM sales_target_fact, time_dim as time
       WHERE time.id=sales_target_fact.time_id AND time.date="2008-01-01"
       GROUP BY time.date
       ORDER BY time.year)
    ) as Combined_Table GROUP BY date ORDER BY year
    

    【讨论】:

    • @Adam ::thanx 的友好回复,这是否意味着 order by 与 group by 不同,只能应用一次?
    • @Sam:查看我编辑的回复,您也可以将 ORDER BY 与每个选择一起使用,方法是将其放在括号内。
    • @Adam,ohhthanx for the reply 即使我询问的问题在阅读了相应的文档后也很清楚,thanx for the help
    • @Adam 但为什么我们需要内括号??
    • @Sam:我猜这是因为第二个 ORDER BY 不明确,除非您提供括号,否则 mySQL 无法判断它是否适用于它后面的 SELECT 或者它是否适用于整个 UNION。我不确定它是否关心第一个 ORDER BY 是否在括号内,但我认为使用并行语法可能是最好的。
    【解决方案2】:

    http://dev.mysql.com/doc/refman/5.1/en/

    也许错误出现在“,time_dim as time”中,但我不确定 - 尝试剪切部分直到错误消失,你会发现错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多