PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹7. 昖臵媆誕嬉誕

7.5. 臯毐废

婘昖臵䫘潊膷庺臘幋劯幘儌滇婘崇䊖垯锬拷彖臘幋劯嘹誻埇傖凹膷庺臘誕臯毐废㔗套悩澇橬毐废闼幽臯儖傖婉埇鵇敋䔇釺废誫啂(垂鍙釺废儖埡喿庯欆柟启誂毖蓇彐䌂傋启婘伕䕻婪䔇釺废嘖滇藇垔婉脘冺蕡認底婩薪)㔗䇞垔䔇釺废埻脘婘滯䇞婄嘪䫘庖毐废準黴幋劯欉脘媺臕㔗

ORDER BY 床埖䫘庯弄滯毐废釺废

SELECT select_list
    FROM table_expression
    ORDER BY sort_expression1 [ASC | DESC] [, sort_expression2 [ASC | DESC] ...]

sort_expression 滇傂嘘埇䫘庯锬拷彖臘䔇臘膆嚟冋套

SELECT a, b FROM table1 ORDER BY a + b, c;

套悩毺垔庖崔婻毐废臘膆嚟闼幽備婘嬉麵䔇臘膆嚟毐废䕩京䔇愙喕婋欉嘪䫘劯麵䔇臘膆嚟啔誕婔準毐废㔗懟婻臘膆嚟鄘埇傖虘婔婻埇锬䔇 ASC(剺废邻螴) 潡 DESC(鍉废)傖螆䘞毐废桹劏㔗剺废噽膷庺償䔇昄唚認麯䔇"償"滇傖 < 淉嘩严䔇蓐庥垔幬䔇㔗䌂嚚䔇滇鍉废滇傖 > 淉嘩严準彴桺䔇㔗[1]

婺庖劏劯噚垹 SQL92 湺庖sort_expression 幘埇傖嘪䫘庖枕劉潡蔙庖枕䚡埙懫套婋麵婴埖

SELECT a + b AS sum, c FROM table1 ORDER BY sum;
SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;

鄘毬䙓丸婔婻庖枕誕臯毐废㔗驔襕濘懟䔇滇膷庺庖枕劉媙釂滇䋸䆋䔇(婉噕螩嘩婺毐废臘膆嚟䔇婔鄘彖)㔗懫套婋麵䔇臺埖滇髍臇䔇㔗

SELECT a + b AS sum, c FROM table1 ORDER BY sum + c;          -- 髍臇䔇

認湙䔇鍊彽婂襕滇婺庖镪噉溓幬㔗埥崡套悩昊婻毐废臘膆嚟脘崘劯施对陉膷庺庖枕劉启臘臘膆嚟婺䔇庖枕劉幘嚔凚躘溓幬(溴施嘪䫘膷庺庖枕劉)㔗嘷䇽認䓉愙喕備婘嘹嘪䫘庖 AS 麉变劉膷庺庖枕幽婫敄喘婯噽垄臘䔇庖枕劯劉䔇施唍欉嚔埏䫘㔗

ORDER BY 埇傖庫䫘庯 UNION, INTERSECT, EXCEPT 䔇螇䞖䂷悩婉誺婘認䓉愙喕婋埻噕螩毬䙓庖枕䔇劉庖潡䚡埙誕臯毐废蔯婉噕螩毬䙓臘膆嚟誕臯毐废㔗

濘懟

[1]

庋垂婪PostgreSQL 嘪䫘邻螴䔇B-tree淉嘩严䌂 婺臘膆嚟䔇昄扞䌂傋䇞垔 ASCDESC毐废釺废㔗婔轸準臘昄扞䌂傋儖赆蘸扵婺锗劽庯 <> 淉嘩严誕臯毐废㔗嘖滇凹庯䫘潙躻垔幬䔇昄扞䌂傋埇傖婉媙套溴㔗


劯锔饡釕嬉誕
䂇劽昖臵婪婔亓LIMITOFFSET