2010-08-10 74 views
105

我正在阅读一些工作中的旧代码,并注意到有几个视图与order by 1子句。这完成了什么?SQL选择语句中的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; 
+5

是的,他可以。但是现在SO有这个问题的答案。 :) – kdmurray 2010-08-10 00:19:03

+5

@Frank,大部分问题都可以通过勤勉的网络搜索来解决。除了事实之外,还有很多东西需要学习这些技术,这就是为什么在SO上进行询问可能是有效的,尽管有明显的网络搜索。居住在人们博客上的未经证实的顾客数量意味着获得第二个意见总是很好的。 – spender 2010-08-10 00:21:41

+3

供参考:在视图中使用ORDER BY通常被认为是不好的做法,除非您可以保证该视图只能用于演示。这很浪费,因为如果在具有ORDER BY的另一个查询中使用视图,ORDER BY将发生2次。 – 2010-08-10 00:27:14

回答

131

此:

ORDER BY 1 

......被称为“序” - 数表示基于SELECT子句中定义的列数列。在您所提供的查询时,它的意思是:

ORDER BY A.PAYMENT_DATE 

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

  1. 不是很明显/显
  2. 如果列的顺序变化,查询仍然有效,以便你可能会通过一些命令你不打算
+0

我的问题,问是否有使用正常的好处:http://stackoverflow.com/questions/2253040/benefits-of-using-sql-ordinal-position-notation – 2010-08-10 00:23:34

+0

这只有'sql'标记。在标准SQL中,'OREDER BY'子句中只允许列相关名称,因为从理论上讲,表相关名称超出了作用域,即应该是'ORDER BY PAYMENT_DATE;'。当然,并不是所有的SQL实现都符合标准。 – onedaywhen 2010-08-10 07:17:12

6

我相信甲骨文是通过列#1

3

意味着为了这会将结果第一列中返回排序。在该示例中,它将按payment_date进行排序。

24

当你使用基于集合的操作符时,这很有用。工会

select cola 
    from tablea 
union 
select colb 
    from tableb 
order by 1; 
+2

啊哈,这很有道理。这是迄今为止我看到的第一个很好的理由。 – echo 2010-08-10 15:00:30

+0

但是'union'暗示了一种隐含的排序? – Lazer 2011-05-29 08:15:57

+2

@Lazer我不这么认为,要执行一个联合,它可能在内部进行排序,但这是一个执行问题,而不是逻辑输出问题,并且本着SQL的精神,不需要按顺序输出行。另外如果你想降序排序?然后你又回到了原来的问题。 – daven11 2011-06-01 01:29:59

-1

从样品测试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 3。注意第三列(PRIVILEGE_TYPE)按字母顺序排序。

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东西─

三通E:/ sqllogfile。文本;

当你完成后,停止测井

开球;

希望它增加更多的清晰度。

3

它仅仅意味着按查询结果的第一列对视图或表进行排序。

1

正如在其他答案中提到ORDER BY 1订单的第一列。

我遇到了另一个可以使用它的例子。我们有一些需要排序的查询,请选择相同的列。如果在下面订购Name,您将得到一个SQL错误。

SELECT Name, Name FROM Segment ORDER BY 1 
+0

你为什么要那样做?为什么不别名呢。 [尽管评论太迟] – 2018-01-21 15:57:30

+0

@abdulqayyum这只是另一种真正做事的方式。上面的例子非常简单。有时,“名称”列实际上是不同表格的不同列,您将插入另一个表格中。添加一堆别名可能会使阅读变得更加困难。使用它的另一个例子是选择许多不同的计算并且想要按一个排序 - 而不需要别名。 (虽然在这里我个人更喜欢别名来说明计算是什么) – nicV 2018-01-23 07:51:02