-- Start
ORDER BY 子句非常简单,正因为简单,很多人不太在意,随意使用ORDER BY 子句很可能出现逻辑错误,考虑如下情况:
CREATE TABLE STUDENT ( ID NUMBER(10, 0) NOT NULL, -- 学号 NAME VARCHAR(20) NOT NULL, -- 姓名 BIRTHDAY DATE -- 生日 ); INSERT INTO STUDENT (ID,NAME,BIRTHDAY) VALUES (1, '张三', DATE '1991-1-1'); INSERT INTO STUDENT (ID,NAME,BIRTHDAY) VALUES (2, '李四', DATE '1991-1-1'); INSERT INTO STUDENT (ID,NAME,BIRTHDAY) VALUES (3, '王五', DATE '1990-1-1');
SELECT * FROM (SELECT * FROM STUDENT ORDER BY BIRTHDAY) WHERE ROWNUM = 1;
SELECT * FROM (SELECT * FROM STUDENT ORDER BY BIRTHDAY, ID) WHERE ROWNUM = 1;
--更多参见:Oracle SQL 精萃
-- 声明:转载请注明出处
-- Last edited on 2015-02-12
-- Created by ShangBo on 2015-02-12
-- End