sql 查詢 提示錯誤:union附近有語法錯誤
原因:
union或者union all只可以有一個order by,要放在後邊的語句
當然,如果有需要用到多個order by 和 union一起使用,也是有辦法的,在文章後面會介紹。
所以union前邊別放排序 order by語句
錯誤:
select * from hovertree_news where hovertreeid <50 order by hovertreeid desc
union
select * from hovertree_news where hovertreeid>100 order by hovertreeid asc
錯誤:
select * from hovertree_news where hovertreeid <50 order by hovertreeid desc
union
select * from hovertree_news where hovertreeid>100
正確:
select * from hovertree_news where hovertreeid <50
union
select * from hovertree_news where hovertreeid>100 order by hovertreeid asc
以下是union和多個order by一起使用,已經在MSSQL中測試通過哦:
select * from (select top 1 * hovertree_news where hovertreeid>54 order by hovertreeid asc) t1
union
select * from (select top 1 * from hovertree_news hovertreeid <54 order by hovertreeid desc) t2