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来标示排序的列