What is the purpose of Order By 1 in SQL select statement?

后端 未结 8 1211
醉酒成梦
醉酒成梦 2020-12-12 19:43

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?

Exampl

相关标签:
8条回答
  • 2020-12-12 20:32

    This:

    ORDER BY 1
    

    ...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. It's not obvious/explicit
    2. If the column order changes, the query is still valid so you risk ordering by something you didn't intend
    0 讨论(0)
  • 2020-12-12 20:39

    This will sort your results by the first column returned. In the example it will sort by payment_date.

    0 讨论(0)
提交回复
热议问题