在SQL select语句中,Order By 1的目的是什么?
我正在阅读一些工作中的旧代码,并且已经注意到,有几个视图用order by 1
子句的order by 1
。 这完成了什么?
例:
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
…被称为“Ordinal” – 数字代表基于SELECT子句中定义的列数的列。 在你提供的查询中,这意味着:
ORDER BY A.PAYMENT_DATE
这不是一个推荐的做法,因为:
- 这不明显/明确
- 如果列顺序发生变化,则查询仍然有效,因此您可能会按照您不想要的方式进行sorting
当你使用基于集合的操作符(例如union)时,这很有用
select cola from tablea union select colb from tableb order by 1;
我相信在甲骨文,这意味着列#1的顺序
这将按照返回的第一列对结果进行sorting。 在示例中,它将按payment_date进行sorting。
它仅仅意味着按查询结果的第一列对视图或表进行sorting。
正如在其他答案中提到的第一列第一个订单。
我遇到了另一个你可能会用到的例子。 我们有一些需要sorting的查询select相同的列。 如果按以下Name
sorting,则会出现SQL错误。
SELECT Name, Name FROM Segment ORDER BY 1
另请参阅:
http://www.techonthenet.com/sql/order_by.php
有关按顺序的说明。 我学到了东西! 🙂
过去我也使用过这个,当我想添加一个不确定数量的filter到一个sql语句。 马虎,我知道,但它的工作。 :P
WAMP服务器数据库示例:
mysql> select * from user_privileges; | GRANTEE | TABLE_CATALOG | PRIVILEGE_TYPE | IS_GRANTABLE | +--------------------+---------------+-------------------------+--------------+ | 'root'@'localhost' | def | SELECT | YES | | 'root'@'localhost' | def | INSERT | YES | | 'root'@'localhost' | def | UPDATE | YES | | 'root'@'localhost' | def | DELETE | YES | | 'root'@'localhost' | def | CREATE | YES | | 'root'@'localhost' | def | DROP | YES | | 'root'@'localhost' | def | RELOAD | YES | | 'root'@'localhost' | def | SHUTDOWN | YES | | 'root'@'localhost' | def | PROCESS | YES | | 'root'@'localhost' | def | FILE | YES | | 'root'@'localhost' | def | REFERENCES | YES | | 'root'@'localhost' | def | INDEX | YES | | 'root'@'localhost' | def | ALTER | YES | | 'root'@'localhost' | def | SHOW DATABASES | YES | | 'root'@'localhost' | def | SUPER | YES | | 'root'@'localhost' | def | CREATE TEMPORARY TABLES | YES | | 'root'@'localhost' | def | LOCK TABLES | YES | | 'root'@'localhost' | def | EXECUTE | YES | | 'root'@'localhost' | def | REPLICATION SLAVE | YES | | 'root'@'localhost' | def | REPLICATION CLIENT | YES | | 'root'@'localhost' | def | CREATE VIEW | YES | | 'root'@'localhost' | def | SHOW VIEW | YES | | 'root'@'localhost' | def | CREATE ROUTINE | YES | | 'root'@'localhost' | def | ALTER ROUTINE | YES | | 'root'@'localhost' | def | CREATE USER | YES | | 'root'@'localhost' | def | EVENT | YES | | 'root'@'localhost' | def | TRIGGER | YES | | 'root'@'localhost' | def | CREATE TABLESPACE | YES | | 'root'@'127.0.0.1' | def | SELECT | YES | | 'root'@'127.0.0.1' | def | INSERT | YES | | 'root'@'127.0.0.1' | def | UPDATE | YES | | 'root'@'127.0.0.1' | def | DELETE | YES | | 'root'@'127.0.0.1' | def | CREATE | YES | | 'root'@'127.0.0.1' | def | DROP | YES | | 'root'@'127.0.0.1' | def | RELOAD | YES | | 'root'@'127.0.0.1' | def | SHUTDOWN | YES | | 'root'@'127.0.0.1' | def | PROCESS | YES | | 'root'@'127.0.0.1' | def | FILE | YES | | 'root'@'127.0.0.1' | def | REFERENCES | YES | | 'root'@'127.0.0.1' | def | INDEX | YES | | 'root'@'127.0.0.1' | def | ALTER | YES | | 'root'@'127.0.0.1' | def | SHOW DATABASES | YES | | 'root'@'127.0.0.1' | def | SUPER | YES | | 'root'@'127.0.0.1' | def | CREATE TEMPORARY TABLES | YES | | 'root'@'127.0.0.1' | def | LOCK TABLES | YES | | 'root'@'127.0.0.1' | def | EXECUTE | YES | | 'root'@'127.0.0.1' | def | REPLICATION SLAVE | YES | | 'root'@'127.0.0.1' | def | REPLICATION CLIENT | YES | | 'root'@'127.0.0.1' | def | CREATE VIEW | YES | | 'root'@'127.0.0.1' | def | SHOW VIEW | YES | | 'root'@'127.0.0.1' | def | CREATE ROUTINE | YES | | 'root'@'127.0.0.1' | def | ALTER ROUTINE | YES | | 'root'@'127.0.0.1' | def | CREATE USER | YES | | 'root'@'127.0.0.1' | def | EVENT | YES | | 'root'@'127.0.0.1' | def | TRIGGER | YES | | 'root'@'127.0.0.1' | def | CREATE TABLESPACE | YES | | 'root'@'::1' | def | SELECT | YES | | 'root'@'::1' | def | INSERT | YES | | 'root'@'::1' | def | UPDATE | YES | | 'root'@'::1' | def | DELETE | YES | | 'root'@'::1' | def | CREATE | YES | | 'root'@'::1' | def | DROP | YES | | 'root'@'::1' | def | RELOAD | YES | | 'root'@'::1' | def | SHUTDOWN | YES | | 'root'@'::1' | def | PROCESS | YES | | 'root'@'::1' | def | FILE | YES | | 'root'@'::1' | def | REFERENCES | YES | | 'root'@'::1' | def | INDEX | YES | | 'root'@'::1' | def | ALTER | YES | | 'root'@'::1' | def | SHOW DATABASES | YES | | 'root'@'::1' | def | SUPER | YES | | 'root'@'::1' | def | CREATE TEMPORARY TABLES | YES | | 'root'@'::1' | def | LOCK TABLES | YES | | 'root'@'::1' | def | EXECUTE | YES | | 'root'@'::1' | def | REPLICATION SLAVE | YES | | 'root'@'::1' | def | REPLICATION CLIENT | YES | | 'root'@'::1' | def | CREATE VIEW | YES | | 'root'@'::1' | def | SHOW VIEW | YES | | 'root'@'::1' | def | CREATE ROUTINE | YES | | 'root'@'::1' | def | ALTER ROUTINE | YES | | 'root'@'::1' | def | CREATE USER | YES | | 'root'@'::1' | def | EVENT | YES | | 'root'@'::1' | def | TRIGGER | YES | | 'root'@'::1' | def | CREATE TABLESPACE | YES | | ''@'localhost' | def | USAGE | NO | +--------------------+---------------+-------------------------+--------------+ 85 rows in set (0.00 sec)
并且当它order by PRIVILEGE_TYPE
被赋予另外的order by PRIVILEGE_TYPE
或者可以order by 3
给出order by 3
。 注意第三列( PRIVILEGE_TYPE
)按字母顺序sorting。
mysql> select * from user_privileges order by PRIVILEGE_TYPE; +--------------------+---------------+-------------------------+--------------+ | GRANTEE | TABLE_CATALOG | PRIVILEGE_TYPE | IS_GRANTABLE | +--------------------+---------------+-------------------------+--------------+ | 'root'@'127.0.0.1' | def | ALTER | YES | | 'root'@'::1' | def | ALTER | YES | | 'root'@'localhost' | def | ALTER | YES | | 'root'@'::1' | def | ALTER ROUTINE | YES | | 'root'@'localhost' | def | ALTER ROUTINE | YES | | 'root'@'127.0.0.1' | def | ALTER ROUTINE | YES | | 'root'@'127.0.0.1' | def | CREATE | YES | | 'root'@'::1' | def | CREATE | YES | | 'root'@'localhost' | def | CREATE | YES | | 'root'@'::1' | def | CREATE ROUTINE | YES | | 'root'@'localhost' | def | CREATE ROUTINE | YES | | 'root'@'127.0.0.1' | def | CREATE ROUTINE | YES | | 'root'@'::1' | def | CREATE TABLESPACE | YES | | 'root'@'localhost' | def | CREATE TABLESPACE | YES | | 'root'@'127.0.0.1' | def | CREATE TABLESPACE | YES | | 'root'@'::1' | def | CREATE TEMPORARY TABLES | YES | | 'root'@'localhost' | def | CREATE TEMPORARY TABLES | YES | | 'root'@'127.0.0.1' | def | CREATE TEMPORARY TABLES | YES | | 'root'@'localhost' | def | CREATE USER | YES | | 'root'@'127.0.0.1' | def | CREATE USER | YES | | 'root'@'::1' | def | CREATE USER | YES | | 'root'@'localhost' | def | CREATE VIEW | YES | | 'root'@'127.0.0.1' | def | CREATE VIEW | YES | | 'root'@'::1' | def | CREATE VIEW | YES | | 'root'@'127.0.0.1' | def | DELETE | YES | | 'root'@'::1' | def | DELETE | YES | | 'root'@'localhost' | def | DELETE | YES | | 'root'@'::1' | def | DROP | YES | | 'root'@'localhost' | def | DROP | YES | | 'root'@'127.0.0.1' | def | DROP | YES | | 'root'@'127.0.0.1' | def | EVENT | YES | | 'root'@'::1' | def | EVENT | YES | | 'root'@'localhost' | def | EVENT | YES | | 'root'@'127.0.0.1' | def | EXECUTE | YES | | 'root'@'::1' | def | EXECUTE | YES | | 'root'@'localhost' | def | EXECUTE | YES | | 'root'@'127.0.0.1' | def | FILE | YES | | 'root'@'::1' | def | FILE | YES | | 'root'@'localhost' | def | FILE | YES | | 'root'@'localhost' | def | INDEX | YES | | 'root'@'127.0.0.1' | def | INDEX | YES | | 'root'@'::1' | def | INDEX | YES | | 'root'@'::1' | def | INSERT | YES | | 'root'@'localhost' | def | INSERT | YES | | 'root'@'127.0.0.1' | def | INSERT | YES | | 'root'@'127.0.0.1' | def | LOCK TABLES | YES | | 'root'@'::1' | def | LOCK TABLES | YES | | 'root'@'localhost' | def | LOCK TABLES | YES | | 'root'@'127.0.0.1' | def | PROCESS | YES | | 'root'@'::1' | def | PROCESS | YES | | 'root'@'localhost' | def | PROCESS | YES | | 'root'@'::1' | def | REFERENCES | YES | | 'root'@'localhost' | def | REFERENCES | YES | | 'root'@'127.0.0.1' | def | REFERENCES | YES | | 'root'@'::1' | def | RELOAD | YES | | 'root'@'localhost' | def | RELOAD | YES | | 'root'@'127.0.0.1' | def | RELOAD | YES | | 'root'@'::1' | def | REPLICATION CLIENT | YES | | 'root'@'localhost' | def | REPLICATION CLIENT | YES | | 'root'@'127.0.0.1' | def | REPLICATION CLIENT | YES | | 'root'@'::1' | def | REPLICATION SLAVE | YES | | 'root'@'localhost' | def | REPLICATION SLAVE | YES | | 'root'@'127.0.0.1' | def | REPLICATION SLAVE | YES | | 'root'@'127.0.0.1' | def | SELECT | YES | | 'root'@'::1' | def | SELECT | YES | | 'root'@'localhost' | def | SELECT | YES | | 'root'@'127.0.0.1' | def | SHOW DATABASES | YES | | 'root'@'::1' | def | SHOW DATABASES | YES | | 'root'@'localhost' | def | SHOW DATABASES | YES | | 'root'@'127.0.0.1' | def | SHOW VIEW | YES | | 'root'@'::1' | def | SHOW VIEW | YES | | 'root'@'localhost' | def | SHOW VIEW | YES | | 'root'@'localhost' | def | SHUTDOWN | YES | | 'root'@'127.0.0.1' | def | SHUTDOWN | YES | | 'root'@'::1' | def | SHUTDOWN | YES | | 'root'@'::1' | def | SUPER | YES | | 'root'@'localhost' | def | SUPER | YES | | 'root'@'127.0.0.1' | def | SUPER | YES | | 'root'@'127.0.0.1' | def | TRIGGER | YES | | 'root'@'::1' | def | TRIGGER | YES | | 'root'@'localhost' | def | TRIGGER | YES | | 'root'@'::1' | def | UPDATE | YES | | 'root'@'localhost' | def | UPDATE | YES | | 'root'@'127.0.0.1' | def | UPDATE | YES | | ''@'localhost' | def | USAGE | NO | +--------------------+---------------+-------------------------+--------------+ 85 rows in set (0.00 sec)
明确地说,一个长的答案和很多滚动。 此外,我努力将查询的输出传递给一个文本文件。 这里是如何做到这一点,而不使用恼人的into outfile
事情 –
Tee E:/sqllogfile.txt;
当你完成后,停止logging –
开球
希望它增加更多的清晰度。