嵌套查询中包含 AS 和 ORDER BY 的 SQL 语法

发布于 2024-08-20 11:33:33 字数 409 浏览 2 评论 0原文

我有一个具有以下语法的查询:

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 语句中。

将其附加到末尾 - 它不喜欢这种语法,并将其放在 as 之前显然是有效的语法,但返回一个空结果集,当省略 order by 时肯定会返回结果。

有什么理论吗?

对变量名称感到抱歉,但它们并不是很重要 - 我关心的更多是关于 order by 的放置。

I have a query with the following syntax:

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...;

I want to now put in an order by into the outer select statement.

attaching it to the end - it doesn't like the syntax, and putting it before the as is apparently valid syntax, but returns an empty result set, when omitting the order by certainly returns results.

Any theories?

Sorry about the variable names, but they're not really important - it's more about the placement of the order by that I'm concerned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音盲 2024-08-27 11:33:33

Order By 位于外部 Select 中的Where 之后。如果没有“Where”,那么您将把它放在连接中最后一个 On (X=X) 选择器之后。

The Order By comes after the Where in the outer Select. If there is no "Where" then you'll place it after the last On (X=X) selector in your join.

飘过的浮云 2024-08-27 11:33:33

您的第一个子查询的真正意思是 order by 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 排序,您能否更好地解释一下您的问题,或者包括您想要开始工作的实际查询。

Did you really mean order by y (a field not included in select) for your first subquery? This bit makes your problem harder. The field you want to order by should be included in the subquery, this way you can do the order by in the outer section.

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

If you did need the order by y for some reason, could you explain your question a bit better, or include the actual query you're trying to get working.

颜漓半夏 2024-08-27 11:33:33

不能放在分号后面,必须放在分号前面

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>;

you can't put it after the semicolon, has to be before it

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>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文