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

7.3. 锬拷彖臘

套嬉麵䔇償誗臘滯䔇闼湙婘 SELECT 变傴婺䔇臘臘膆嚟锔誺䂇劽臘㔕蓖商㔕役鍴臯㔕彖䂇京悇锹庖婔婻婺傋攓䔇荔拘臘㔗認婻臘橔劯嚹锐䂍锬拷彖臘崇䊖㔗锬拷彖臘彴桺橔䂽垂鍙膷庺荔拘臘䔇巻底庖枕

7.3.1. 锬拷彖臘釹

橔䞔剘䔇锬拷彖臘滇 * 垄膷庺臘臘膆嚟䫘潊䔇欔橬庖枕㔗劥彍婔婻锬拷彖臘滇婔婻锖埙彖锫䔇唚臘膆嚟䔇彖臘(启婘誗4.2麯垔幬䔇婔湙)㔗懫套垄埇脘滇婔婻庖枕劉彖臘

SELECT a, b, c FROM ...

庖枕劉 a, b, c 襕幽滇婘 FROM 床埖麯嚘䫘䔇臘婺庖枕䔇垂鍙劉庖襕幽滇誗7.2.1.2麯蓼麪䔇彆劉㔗锬拷彖臘婺䔇劉庖䷺閘启 WHERE 床埖婺䔇劉庖䷺閘滇婔湙䔇鍴麂嘹嘪䫘庖彖䂇劥彍垄启 HAVING 床埖婺䔇劉庖䷺閘幘婔湙㔗

套悩崔婻臘橬麉崉䔇庖枕劉闼幽嘹誻媙釂䂍庺臘劉庖冋套

SELECT tbl1.a, tbl2.a, tbl1.b FROM ...

嘷嘪䫘崔婻臘施䂍庺臘劉誻橬媷庯嚘䫘臖臘䔇欔橬庖枕

SELECT tbl1.*, tbl2.a FROM ...

埽蓕誗7.2.2

套悩儖唚臘膆嚟䫘庯锬拷彖臘闼幽垄婘楗媕婪劏誫啂䔇臘婺嵂媹庖婔婻桄䔇荔拘庖枕㔗唚臘膆嚟婺䂷悩婺䔇懟婔臯誕臯婔渇螇䞖螇䞖幋嬉䫘臖臯䔇昄唚敪扵傂嘘臘膆嚟麯嚘䫘䔇庖枕㔗婉誺锬拷彖臘婺䔇認婻臘膆嚟幽麂婔垔襕嚘䫘準躻 FROM 床埖婺臘臘膆嚟麯麵䔇庖枕懫套垄幘埇傖滇傂懟婩麟䞖橇臘膆嚟㔗

7.3.2. 庖枕湺了

锬拷彖臘婺䔇彖臘釹埇傖蕋庽劉庖傖冪庯誕婔準䔇崇䊖㔗認麯"誕婔準崇䊖"滇毺婔婻埇锬䔇毐废弄滯启垵潙䆇庫䫘(懫套䫘庯滆䴺䔇庖枕崘)㔗懫套

SELECT a AS value, b + c AS sum FROM ...

套悩澇橬嘪䫘 AS 弄滯庖枕劉庖闼幽係䂘儖蕋庽婔婻䚺䩕唚㔗凹庯䞔剘䔇庖枕嚘䫘垄滇臖庖枕䔇劉庖㔗凹庯庘昄脄䫘垄滇臖庘昄䔇劉庖㔗凹庯崉溗臘膆嚟係䂘嚔䫘潊婔婻锔䫘䔇劉庖㔗

㔊濘懟㔏膷庺庖枕䔇变劉启婘 FROM 床埖麯䔇变劉滇婉婔湙䔇(埗黙誗7.2.1.2)㔗認湙儌噕螩嘹凹劯婔婻庖枕变劉婴渇FROM 床埖麯䔇劉庖儖赆锬拷彖臘嘪䫘蔯锬拷彖臘婺桄埡䔇劉庖儖赆橔䂽膷庺㔗

7.3.3. DISTINCT

婘崇䊖垯锬拷彖臘幋劯䫘潊䔇臘埇傖役鍴麉崉臯㔗䕘毖婘 SELECT 劯麵喍婪 DISTINCT 噿髞庖剿埇

SELECT DISTINCT select_list ...

套悩婉䫘 DISTINCT 嘹埇傖䫘 ALL 弄滯媺䘍欔橬臯䔇䚺䩕臯婺㔗

滆䇽套悩婴臯麯躿儏橬婔婻庖枕唚婉劯闼幽潏傸螴婺認婴臯滇䋸䆋䔇㔗NULL 婘認麯赆螴婺滇䕩劯䔇㔗

埥崡潏傸誻埇傖䫘臘膆嚟準彴桺傔幽湙䔇臯埇傖螴婺滇䋸䆋䔇

SELECT DISTINCT ON (expression [, expression ...]) select_list ...

認麯䔇 expression 滇婔婻唚臘膆嚟垄婺懟婔臯螇䞖㔗套悩婔䂇臯螇䞖庺䔇臖臘膆嚟䔇唚鄘䕩劯闼幽儌螴婺認底臯滇麉崉䔇幽埻膷庺丸婔臯㔗臙濘懟認麯䔇"丸婔臯"滇婉埇鵇桍䔇鍴麂嘹婘轿崘崔䔇庖枕婪凹臖昖臵誕臯庖毐废媺臕彄膆 DISTINCT 誺悴単施臯䔇釺废滇嫇婔䔇(DISTINCT ON 儖婘 ORDER BY 毐废幋劯崇䊖)㔗

DISTINCT ON 床埖婉滇 SQL 湺庖䔇婔鄘彖橬施唍赆滇婔婻俘俘䔇鼯湚啹婺垄䔇䂷悩滇婉埇彴垔䔇㔗套悩䫘橬埇锬䔇 GROUP BY 启婘 FROM 婺䔇床昖臵埇傖膆彄䕞䔇闼幽潏傸埇傖镪噉嘪䫘認婻悇锹嘖滇锔婩垄滇敘桹冪䔇桹濘㔗


劯锔饡釕嬉誕
臘臘膆嚟婪婔亓䂇劽昖臵