zoukankan      html  css  js  c++  java
  • 如何联合使用Union和Order by

    如果使用类似下面的SQL语句:

    select columnA, columnB from tableA where columnA = 'Condition 1'
    union
    select columnC, columnD from tableB where columnC = 'Condition 1'
    order by columnA asc

     系统会报错:
    消息 207,级别 16,状态 1,第 4 行
    列名 'columnA' 无效。
    消息 104,级别 16,状态 1,第 4 行
    如果该语句包含 UNION、INTERSECT 或 EXCEPT 运算符,则 ORDER BY 项必须出现在选择列表中。

    问题原因:
    既然把多行给union了,把每列叫成什么名字都是不妥当的。。。只要采用列的序号即可。

    select columnA, columnB from tableA where columnA = 'Condition 1'
    union
    select columnC, columnD from tableB where columnC = 'Condition 1'
    order by 2 asc

    这里需要注意的是,序号从1开始,如果指定的序号不在允许的范围内,系统会报出一个异常

  • 相关阅读:
    gdb高级技巧
    Fira Code字体安装与配置
    回归
    【Luogu】P2292 [HNOI2004]L语言 题解
    浅谈Linux桌面(发行版及桌面环境)
    剑指offer-和为S的连续正数序列-知识迁移能力-python
    剑指offer-数组中只出现一次的数字-数组-python
    剑指offer-数字在排序数组中出现的次数-数组-python
    剑指offer-数组中的逆序对-数组-python
    剑指offer-丑数-穷举-python
  • 原文地址:https://www.cnblogs.com/KiloNet/p/2622050.html
Copyright © 2011-2022 走看看