Oracle Union/Union all 的排序问题

  • Post author:
  • Post category:其他


Oracle 通过在索引列上使用UNION/UNION ALL来提高合并查询的效率(相对于OR,不在此具体阐述),UNION/UNION ALL排序时,无论有多少个被union的部分,都只有最后一行一个order by子句,且UNION/UNION ALL的order by子句只能通过列号来标示你要排序的字段,例如:

SELECT a.recID, a.eventLevel, a.srcIP, a.dstIP, a.eventTime, a.eventCount, a.srcPort, a.dstPort, a.autoProcessed,

b.popular, b.eventName, b.popularName, b.securityID,

d.deviceIP,d.deviceName

FROM NETIDS_EVENTLOG_HISTORY a, NETIDS_DEFINE_EVENTBASE b, SYSTEM_DEPLOY d

WHERE a.eventTypeID=b.eventTypeID AND a.DEV_GUID=d.DEV_GUID

–order by a.eventtime desc 在此处加order by子句是错误的!!

UNION ALL


SELECT a.recID, a.eventLevel, a.srcIP, a.dstIP, a.eventTime, a.eventCount, a.srcPort, a.dstPort, a.autoProcessed,

b.popular, b.eventName, b.popularName, b.securityID,

e.deviceIP, e.deviceName

FROM NETIDS_EVENTLOG a, NETIDS_DEFINE_EVENTBASE b, SYSTEM_DEPLOY e

WHERE a.eventTypeID=b.eventTypeID AND a.DEV_GUID=e.DEV_GUID

order by 5 desc

–要用a.eventtime排序,只能在最后一行使用order by,且使用a.eventtime的列号——5来标示排序的列



版权声明:本文为tomxiaodong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。