PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹2. SQL 臺蘔媆誕嬉誕

2.5. 昖臵婔婻臘

襕傯婔婻臘婺演䘵昄扞儌滇昖臵認婻臘㔗SQL 䔇 SELECT 臺埖儌滇啔認婻䫘锫䔇㔗臖臺埖彖婺锬拷彖臘(彖庺襕誫啂䔇庖枕)㔕臘彖臘(彖庺傯婺演䘵昄扞䔇臘)㔕傖埪埇锬䔇溇傽(弄滯傂懟鍊彽)㔗懫套襕演䘵臘 weather 䔇欔橬臯髞噖

SELECT * FROM weather;

認麯䔇 * 滇"欔橬庖枕"䔇䚷喍㔗[1]啹溴劯湙䔇䂷悩埇傖䫘婋麵䔇臺埖诙冖

SELECT city, temp_lo, temp_hi, prcp, date FROM weather;

蔯膷庺庫臖滇

     city      | temp_lo | temp_hi | prcp |    date
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      43 |      57 |    0 | 1994-11-29
 Hayward       |      37 |      54 |      | 1994-11-29
(3 rows)

嘹埇傖婘锬拷彖臘婺喍傂懟臘膆嚟蔯婉備備滇庖枕彖臘㔗懫套嘹埇傖

SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;

認湙庫臖冖彄

     city      | temp_avg |    date
---------------+----------+------------
 San Francisco |       48 | 1994-11-27
 San Francisco |       50 | 1994-11-29
 Hayward       |       45 | 1994-11-29
(3 rows)

臙濘懟認麯䔇 AS 床埖滇套嘘䂍膷庺庖枕麉桄变劉䔇㔗AS 床埖滇埇锬䔇㔗

婔婻昖臵埇傖嘪䫘 WHERE 床埖誕臯"媞閄"弄滯驔襕巻底臯㔗WHERE 床埖寙劆婔婻婄儫臘膆嚟埻橬闼底婄儫臘膆嚟婺䩘䔇臯欉嚔赆誫啂㔗噕螩嘹婘溇傽婺嘪䫘婩䫘䔇婄儫淉嘩严(AND, OR, NOT)㔗懫套婋麵䔇昖臵演䘵斓麏匌䔇婋镘崷䔇崷愫

SELECT * FROM weather
    WHERE city = 'San Francisco' AND prcp > 0.0;

䂷悩

     city      | temp_lo | temp_hi | prcp |    date
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
(1 row)

嘹埇傖襕挗誫啂䔇昖臵滇毐喘废䔇

SELECT * FROM weather
    ORDER BY city;
     city      | temp_lo | temp_hi | prcp |    date
---------------+---------+---------+------+------------
 Hayward       |      37 |      54 |      | 1994-11-29
 San Francisco |      43 |      57 |    0 | 1994-11-29
 San Francisco |      46 |      50 | 0.25 | 1994-11-27

婘認婻冋床麯毐废䔇釺废幽麂䂺凹橙捄䔇啹溴嘹埇脘䩋彄 San Francisco 臯锟橺䔇毐废㔗嘖滇套悩嘹嘪䫘婋麵䔇臺埖闼幽儌攂滇嚔冖彄婪麵䔇䂷悩

SELECT * FROM weather
    ORDER BY city, temp_lo;

嘹埇傖襕挗昖臵䔇䂷悩毬䙓昊䓉釺废毐废幽婫潽鍴麉崉臯䔇膷庺

SELECT DISTINCT city
    FROM weather;
     city
---------------
 Hayward
 San Francisco
(2 rows)

喉渇弄滯䂷悩臯䔇釺废埇脘滇锟橺䔇㔗嘹埇傖䂇劽嘪䫘 DISTINCTORDER BY 準诙埡婔躘䔇䂷悩[2]

SELECT DISTINCT city
    FROM weather
    ORDER BY city;

濘懟

[1]

荘䇽 SELECT * 埇傖檪昖臵䔇䂷悩䪂庺準嘖潏傸捞镉螴婺婘䫘库傼乕婺認滇冽俘俘䔇鼯湚啹婺䂍臘嵂媹婔婻庖枕儌櫹埻庖䂷悩㔗

[2]

婘婔底昄扞康係䂘麯寙拸蔕䬽橸䔇 PostgreSQL DISTINCT 躻媘凹臯誕臯毐废啹溴 ORDER BY 滇崔嘍䔇㔗嘖滇認婔䗹幽婉滇 SQL 湺庖䔇襕挗幽婫䕞嬉䔇 PostgreSQL 幽婉媺臕 DISTINCT 凚躘昄扞臯赆毐废㔗


劯锔饡釕嬉誕
劏臘婺時媹臯婪婔亓婘臘幋閘誂毖