2012-07-05 8 views
12

私はMySQLで質問しましたが、正しく行いました。しかし、本のコードは少し異なります。この注文は何ですか?

帳:

use tennis; 
select playerno, datediff(coalesce(end_date, current_date), 
begin_date) as Difference, position 
from committee_members 
where datediff(coalesce(end_date, current_date), begin_date) > 500 
order by 1; 

1この順とは何ですか?

私のコードを除いても、作品とほぼ同じである:

select playerno, datediff(coalesce(end_date, current_date) AS Data, 
order by Data; 
+0

にStackOverflow - フォーマットになぜuが容易ではありません? : – Master

+2

書式設定のヘルプを読む限り、書式を設定するのは簡単です。 – Ryan

+0

コードブロックを作るために行の先頭に4つのスペースを追加するだけです。しかし、最初の行の前に空白行があることを確認してください。エディタのヘルプアイコンについて詳しくは、こちらをご覧ください。 – rcdmk

答えて

23

order by 1手段「私が選択した最初のフィールドによって順序」 - すなわち、この場合には、order by playernoと同じ、playernoだったので、リストの最初のフィールド。

編集:SQL-92 standardのドラフトで簡単に見をしているが、これを確認した。この場合、

10)If ORDER BY is specified, then each <sort specification> in the 
     <order by clause> shall identify a column of T. 

     Case: 

     a) If a <sort specification> contains a <column name>, then T 
      shall contain exactly one column with that <column name> and 
      the <sort specification> identifies that column. 

     b) If a <sort specification> contains an <unsigned integer>, 
      then the <unsigned integer> shall be greater than 0 and not 
      greater than the degree of T. The <sort specification> iden- 
      tifies the column of T with the ordinal position specified by 
      the <unsigned integer>. 

bを適用すると思われるものです。このドラフトと標準の最終文章との間には少なくともいくつかの変更があると確信していますが(標準版と別の版の間には言及していませんが)、この基本的なことが(おそらくこれまでに)変わることは考えにくいでしょう。

+0

ありがとう!とった !すてきでクリア! – Master

+0

準拠規格ではなく、MySQL独自のドキュメントでバックアップしましょう:https://dev.mysql.com/doc/refman/5.7/en/select.htmlキーワード: 'order by 2' – Sinkeat

5

これは、 "ORDER BY ordinal"と呼ばれ、基本的にはその位置の列で順序付けされます。 1での注文は、最初に選択した列による注文を意味します。あなたの例では、ORDER BY playerno

と言っているのと同じになりますが、どのような列が参照されているかがわからないため、列の順序が変更された場合にクエリが異なる結果を返します。

その他のリソース:

Quick Tip: Order By 1 Desc

Bad habits to kick : ORDER BY ordinal

SQL: order by

+0

..... .........正確に! – Master