【问题标题】:SQL ORDER BY with UNIONSQL ORDER BY 与 UNION
【发布时间】:2014-09-26 13:16:53
【问题描述】:

我正在尝试基于现有表创建视图。 该表将如下所示:

+------+------+------+------+
| col1 | col2 | col3 | col4 |
+------+------+------+------+
| 1    | a1   | a2   | a3   |
| 2    | b1   | b2   | b3   |
| 3    | c1   | c2   | c3   |
| 4    | d1   | d2   | d3   |
| 5    | e1   | e2   | e3   |
| 6    | f1   | f2   | f3   |
+------+------+------+------+

结果视图应具有以下方式的行:

+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| 1    | a1   | a3   |
| 2    | a2   | a3   |
| 3    | b1   | b3   |
| 4    | b2   | b3   |
| 5    | c1   | c3   |
| 6    | c2   | c3   |
+------+------+------+

我的 SQL 查询如下所示:

(select col1 as "col 1",col2 as "col 2",col4 as "col 3" from t1)
union
(select col1 as "col 1",col3 as "col 2",col4 as "col 3" from t1)
order by "col 2","col 3"

order by 似乎不起作用。对于order by 之后给出的任何别名,我得到的行顺序相同

提前致谢

【问题讨论】:

  • 你确定你发布的结果表真的是你想要的吗?源中没有 col1=2 且 a2 和 a3 并排出现的行。
  • 我不知道 MySQL 中的反引号,感谢 Andrity M 指出这一点。还有一个模棱两可的别名,重命名它解决了这个问题。谢谢大家的回答。

标签: mysql sql sql-order-by union


【解决方案1】:

您的 order by 是指定两个常量字符串,而不是包含空格的列名。试试

select col1, col2, col4 as col3 from t1
union
select col1, col3 as col2, col4 as col3 from t1
order by col2, col3

这仍然不会为您重新排序 col1。

【讨论】:

  • 我的别名需要空格。我不能改变它。
【解决方案2】:

试试:

select * from (
  (select col1 as col1,col2 as col2,col4 as col3 from t1)
  union
  (select col1 as col1,col3 as col2,col4 as col3 from t1)
) as t
order by t.col2,t.col3

在您的情况下,只有第二个查询会对结果进行排序。

【讨论】:

  • 试过了,它给出了一个错误“错误代码:1248。每个派生表都必须有自己的别名”
  • 如果我添加反引号而不是双引号 (t.col2),较早的答案(在此编辑之前)也有效
【解决方案3】:
SELECT AId AS A1, BId AS S2, CId  AS S3 FROM(
    SELECT AId, BId, CId FROM TableName
    UNION
    SELECT AId, BId, CId FROM TableName
) AS T ORDER BY S2, S3 DESC

但是为什么需要两个相同记录集中的联合呢?

【讨论】:

  • 试了一下,报错:“Error Code: 1060. Duplicate column name 'col 2'”
【解决方案4】:

应该这样做:

SELECT COL_1, COL_2, COL_3 
FROM ((SELECT COL1 AS COL_1, COL2 AS COL_2, COL4 AS COL_3 FROM T1) 
UNION (SELECT COL1 AS COL_1, COL3 AS COL_2 , COL4 AS COL_3 FROM T1)) 
ORDER BY COL_1, COL_2

或者就这样:

(SELECT COL1 AS COL_1, COL2 AS COL_2, COL4 AS COL_3 FROM T1) 
UNION 
(SELECT COL1 AS COL_1, COL3 AS COL_2 , COL4 AS COL_3 FROM T1)
ORDER BY COL_1, COL_2

【讨论】:

    【解决方案5】:

    很可能您的 MySQL 实例配置为treat double-quoted strings as string literals rather than quoted identifiers。如果你使用 MySQL 的原生反引号来分隔 ORDER BY 中的名称,you will get the expected result:

    (select col1 as "col 1",col2 as "col 2",col4 as "col 3" from t1)
    union
    (select col1 as "col 1",col3 as "col 2",col4 as "col 3" from t1)
    order by `col 2`,`col 3`
    

    【讨论】:

    • 这给了我一个错误:“错误代码:1052. order 子句中的列 'col 2' 不明确”
    • 你把它弄得模棱两可了。你可以看到我的演示没有遇到这个问题。可能您使用的别名名称已经是另一列的名称(在您的 real 查询中)。无论如何,这与您最初提出的问题完全不同。
    • 谢谢!! 一个模棱两可的别名。我的实际查询有 40 列,我查看了每个别名,然后重命名了模棱两可的一个并且它起作用了。
    【解决方案6】:

    试试这个

    (select col1 as c1,col2 as c2,col4 as c3 from t)
    union
    (select col1 as c1,col3 as c2,col4 as c3 from t)
    order by c2,c3
    

    Demo

    【讨论】:

      猜你喜欢
      • 2010-10-04
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 2010-09-17
      • 2020-01-18
      相关资源
      最近更新 更多