簡體   English   中英

嵌套查詢中帶有AS和ORDER BY的SQL語法

[英]SQL Syntax with AS and ORDER BY in nested queries

我有一個查詢語法如下:

select x.a as a, x.b as b, x.c as c
from
  (select distinct a from foo
     order by y) as x
  left join zzz....
  left join yyy...;

我想現在order by外部select語句輸入一個order by

將它附加到結尾 - 它不喜歡語法,並將它放在as之前顯然是有效的語法,但返回一個空的結果集,當省略order by肯定會返回結果。

任何理論?

對變量名稱感到抱歉,但它們並不重要 - 它更多的是關於我關注的order by的位置。

Order By位於外部選擇中的Where之后。 如果沒有“Where”,那么您將把它放在連接中的最后一個On(X = X)選擇器之后。

你真的是指第一個子查詢的y(不包括在select中的字段)的順序嗎? 這一點會讓你的問題變得更難。 您要訂購的字段應包含在子查詢中,這樣您就可以在外部區域中執行訂單。

select x.a as a, x.b as b, x.c as c
from
  (select distinct a from foo) as x
  left join zzz....
  left join yyy...
order by x.a

如果由於某種原因你確實需要y的訂單,你能否更好地解釋你的問題,或者包括你想要工作的實際查詢。

你不能把它放在分號之后,必須在它之前

select x.a as a, x.b as b, x.c as c
from
  (select distinct a from foo
     order by y) as x
  left join zzz....
  left join yyy...
order by <column list>;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM