gpt4 book ai didi

mysql - 使用 UNION ALL 和 ORDER BY 选择

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:52 25 4
gpt4 key购买 nike

我尝试使用 UNION ALL 从几个表中进行查询,但我在 ORDER 子句上遇到此错误:

#1054 - Unknown column 'Time' in 'order clause'

我在表中有列 Time。这是查询:

SELECT * from
(SELECT table, 'as' from as
UNION ALL
SELECT table, 'as' from as1
UNION ALL
SELECT table, 'as' from as2
UNION ALL
SELECT table, 'as' from as3) asAllWrong
WHERE table not like 'as%' OR length(table) < 12
ORDER BY Time='2015-06-02 9:00:00;

那么,我究竟该如何查询才能显示这些表和该表中的所有错误条目?为什么即使有一个名为 Time 的列,我也会收到此错误?

编辑:我的错误是它们是不同的表,如 as1、as2...我想像我所说的那样查询当时所有错误的条目。

编辑:这就是现在的样子并且到目前为止运行良好。请注意,这是给我的……不会用于任何生产,有时会使用,所以我真的不需要性能……等等。

SELECT * from
( SELECT as.*, 'as' from as
UNION ALL
SELECT as1.*, 'as1' from as1
UNION ALL
SELECT as2.*, 'as2' from as2
UNION ALL
SELECT as3.*, 'as3' from as3) asAllWrong
WHERE as not like 'as%' OR length(as) < 12
ORDER BY Time='2015-06-02 9:00:00' ASC;

最佳答案

虽然查询看起来很困惑,但底线是您通过 UNION ALL 创建的结果内存表不包含字段 Time,这就是为什么您有一个错误。

至于为什么不存在,通常你不能只是select {table_name}。它必须是一个列列表。但我不是 MySql 的专家——它可能允许这样的诡计。正如乔纳森在下面指出的那样,你最好使用:

select table.Time, 'as' from table
union all...

这肯定会将 Time 字段带入画面。

关于mysql - 使用 UNION ALL 和 ORDER BY 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30588851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com