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;
ベストアンサー1
order by 1
は「最初に選択したフィールドで並べ替える」という意味です。つまり、この場合はがリストの最初のフィールドであるorder by playerno
ため、 と同じです。playerno
正式な文言を知りたい場合は、SQL-92 標準1 は言う:
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
が当てはまるようです。
ただし、SQL 標準の最新バージョンではこの機能は削除されているため、新しいコードでは一般的にこれを避ける必要があります。SQL ベースのデータベース サーバーでは、この機能はしばらく前から非推奨になっていますが、ほとんどのサーバーでは下位互換性のために引き続きサポートされています。同時に、非推奨になったということは、サーバー側でこの機能を本当にサポートする必要がなくなったことを意味しており、いつでも警告なしに削除される可能性があります (たとえば、コードのその部分にバグが見つかった場合、そのバグを修正する最善の方法は、その機能を無効にすることだと判断するかもしれません)。
1. この引用は、承認された標準ではなく、無料で入手できる草案からのものです。この草案と標準の最終版の間には少なくともいくつかの変更点があると思いますが (標準のバージョン間の違いは言うまでもありません)、草案と最終版の間でこれほど根本的な変更が行われる可能性は低いと思われます。