本文介绍了SQL select 语句中 Order By 1 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一些工作中的旧代码,并注意到有几个带有 order by 1 子句的视图.这有什么作用?

I'm reading through some old code at work, and have noticed that there are several views with an order by 1 clause. What does this accomplish?

示例:

Create view v_payment_summary AS
SELECT A.PAYMENT_DATE,
       (SELECT SUM(paymentamount)
          FROM payment B
         WHERE PAYMENT_DATE = B.PAYMENT_DATE
           and SOME CONDITION) AS SUM_X,
       (SELECT SUM(paymentamount)
          FROM payment B
         WHERE PAYMENT_DATE = B.PAYMENT_DATE
           and SOME OTHER CONDITION) AS SUM_Y
FROM payment A
ORDER BY 1;

推荐答案

这个:

ORDER BY 1

...被称为序数"——数字代表基于 SELECT 子句中定义的列数的列.在您提供的查询中,这意味着:

...is known as an "Ordinal" - the number stands for the column based on the number of columns defined in the SELECT clause. In the query you provided, it means:

ORDER BY A.PAYMENT_DATE

这不是推荐的做法,因为:

It's not a recommended practice, because:

  1. 这并不明显/明确
  2. 如果列顺序发生变化,查询仍然有效,因此您可能会按您不希望的顺序进行排序

这篇关于SQL select 语句中 Order By 1 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:42