代码之家  ›  专栏  ›  技术社区  ›  Mark Mayo

嵌套查询中带有AS和ORDER BY的SQL语法

  •  3
  • Mark Mayo  · 技术社区  · 16 年前

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

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

    订购人 当然会返回结果。

    有什么理论吗?

    对于变量名,我很抱歉,但它们并不是很重要——更多的是关于变量的位置 订购人

    3 回复  |  直到 16 年前
        1
  •  3
  •   Mark Brittingham    16 年前

    Order By位于外部选择中的Where之后。如果没有“Where”,则将其放在联接中最后一个On(X=X)选择器之后。

        2
  •  0
  •   Richard    16 年前

    对于第一个子查询,您真的是指按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
    

        3
  •  -1
  •   SQLMenace    16 年前

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

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