嵌套查询中包含 AS 和 ORDER BY 的 SQL 语法
我有一个具有以下语法的查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
您的第一个子查询的真正意思是 order by y (不包含在 select 中的字段)吗?这一点让你的问题变得更加困难。您想要排序的字段应该包含在子查询中,这样您就可以在外部部分中进行排序。
如果您出于某种原因确实需要按 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.
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.
不能放在分号后面,必须放在分号前面
you can't put it after the semicolon, has to be before it