首页 > 数据库 >SQL报错:order by不能直接出现在union的子句中

SQL报错:order by不能直接出现在union的子句中

时间:2024-03-04 23:47:33浏览次数:26  
标签:union month ASC 报错 子句 order ORDER SELECT

原文链接:https://www.cnblogs.com/xuwinwin/p/15877703.html

1、报错写法:

SELECT * FROM t1  ORDER BY month ASC
UNION
SELECT * FROM t2  ORDER BY month ASC

2、原因与解决办法:

order by不能直接出现在union的子句中,但是可以出现在子句的子句中。

可以通过两个查询分别加括号的方式,改成如下:

(SELECT * FROM t1  ORDER BY month ASC)
UNION
(SELECT * FROM t2  ORDER BY month ASC)

  这种方式的目的是为了让两个结果集先分别order by,然后再对两个结果集进行union。虽然这种方式不报错,但是两个order by并没有效果,这是因为联合查询的结果是整个查询完成后得出的,而不是将子查询挨个完成后拼接的

改成如下:

 

SELECT * FROM
(SELECT * FROM t1  ORDER BY month ASC) t3
UNION
SELECT * FROM
(SELECT * FROM t2 ORDER BY month ASC) t4

 

  

 

 

标签:union,month,ASC,报错,子句,order,ORDER,SELECT
From: https://www.cnblogs.com/Dongmy/p/18053028

相关文章

  • PostgreSQL 在使用连表语句时报错 ERROR: operator does not exist: bigint = charact
    背景在使用PostgreSQL数据库过程中,使用了连表语句如下所示,其中a表的order_no为bigint类型,b表的order_no为varchar类型select*fromtable_orderainnerjointable_order_itembona.order_no=b.order_no;遇到提示:ERROR:operatordoesnotexist:bigint=characterv......
  • vite+vue3 遇到报错 Uncaught SyntaxError: Cannot use import statement outside a m
    按照报错找到了对应的位置import{createApp}from'/node_modules/.vite/deps/vue.js?v=d0a669cf'importAppfrom'/src/pages/project1/App.vue'//import'./index.css'//importrouterfrom"./router"//createApp(App).mount(&#......
  • 关于SAP-APP机器-R3trans -d报错-R3trans: /lib64/libstdc++.so.6: version `GLIBCXX_
    在SAP-应用-APP-机器上执行如下命令报错awpxxx03:prdadm270>R3trans-dR3trans:/lib64/libstdc++.so.6:version`GLIBCXX_3.4.26'notfound(requiredbyR3trans) 其实之前,使用过一种方法解决这个问题,可以参考笔者另一篇文章《关于Redhat-Linux中-compat-sap-c++的说......
  • pip install selenium报错 raise ReadTimeoutError--解决方法
    pipinstallselenium报错如下:raiseReadTimeoutError(self._pool,None,"Readtimedout.")pip._vendor.urllib3.exceptions.ReadTimeoutError:HTTPSConnectionPool(host='files.pythonhosted.org',port=443):Readtimedout. 原因:timeout超时,默认15s解......
  • nacos报错
    1.nacos日志报错com.alibaba.nacos.api.exception.runtime.NacosRuntimeException:ErrCode:500,ErrMsg:Userlimitofinotifywatchesreached解决:这个错误是由于Linux中inotify观察者数量达到上限导致的。在Linux系统中,inotify是用来监视文件系统事件的机制,当监视的文......
  • JSON.parse解析字符串报错-SyntaxError: Unexpected token ‘ in JSON at position 报
    “SyntaxError:Unexpectedtoken’inJSONatposition”报错原因是因为解析的字符串对象中,JSON.parse无法识别;JSON.parse可以将标准的json类型数据转换为JavaScript对象,如果数据不是正确的json类型的数据则会控制台报错,可能会阻断代码的正常运行我们可以写一个函数来......
  • shell工具连接linux时的报错问题
    问题描述在使用shell工具连接linux时报以下错误SSH!Agentauthselected,butnorunningagentisdetectedSSH!Agentauthselected,butnorunningagentisdetected解决方法方法1#首先使用ping方法查看是否在同一网段pingxxx.xxx.xx.x方法2#安装[openss......
  • pytorch报错:Variable._execution_engine.run_backward( # Calls into the C++ engine
    GPU模式下运行pytorch代码报错,pytorch为2.2.1,NVIDIA驱动版本535.161.07File"/home/devil/anaconda3/envs/sample-factory/lib/python3.11/site-packages/torch/_tensor.py",line522,inbackwardtorch.autograd.backward(File"/home/devil/anaconda3/envs/sample-......
  • 解决ssh链接报错问题
    在使用ssh连接时出现报错,如下┌──(kali㉿kali)-[~]└─[email protected]:nomatchinghostkeytypefound.Theiroffer:ssh-rsa,ssh-dss意思就是找不到匹配的主机密钥类型,需要手动......
  • Linux_Centos_yum报错总结
    ​此篇适用于yum报错【尝试其他镜像】并且【curl外网】不通的情况,此时一般考虑是网络的问题一,出现的报错信息: 此时测试curl/pingwww.baidu.com会发现无法连通 二,解决方法:1,首先查看dns的配置文件/etc/resolv.conf检查这里的nameserver这里有时候会因为第二个网卡......