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

7.2. 臘臘膆嚟

臘臘膆嚟 螇䞖婔婻臘垄寙劆婔婻 FROM 床埖臖床埖埇傖湹扞驔襕锬䫘 WHERE, GROUP BY, HAVING 床埖㔗崓鄘彖臘臘膆嚟埻滇毺劏伕䕻婪䔇婔婻欔脷䔇嘺橸臘嘖滇潏傸埇傖䫘敘崉溗䔇臘臘膆嚟傖劇䓉桹濘媞櫹潡䂇劽嘺橸臘㔗

臘臘膆嚟麯䔇 WHERE, GROUP BY, HAVING 床埖弄滯婔係彖凹溊躻 FROM 床埖䔇臘䔇蘸扵淉嘩㔗欔橬認底蘸扵橔劯䫘潊婔婻荔拘臘嚹锐䂍锬拷彖臘螇䞖膷庺臯㔗

7.2.1. FROM 床埖

FROM 床埖 傯婔婻锖埙彖锫䔇臘嚘䫘彖臘婺䫘潊婔婻荔拘臘㔗

FROM table_reference [, table_reference [, ...]]

臘嚘䫘埇傖滇婔婻臘劉庖(埇脘橬昇嚟媞閄)潡蔙滇婔婻䫘潊䔇臘懫套床昖臵㔕臘誂毖㔕潡認底婩薪䔇崉溗䂇劽㔗套悩婘 FROM 床埖婺彖庺庖崔庯婔婻臘闼幽垄傸赆庴埬誂毖(蓕婋桺)嘵潊婔婻昆䫘臘臖臘埇傖誕臯 WHERE, GROUP BY, HAVING 床埖䔇蘸扵崇䊖幽橔劯䫘潊臘臘膆嚟䔇䂷悩㔗

套悩婔婻臘嚘䫘滇婔婻䞔剘䔇佽臘䔇劉庖闼幽儖寙拸噽欔橬劯傼床臘䔇臯鍴麂嘹婘臖臘劉庖嬉麵媹 ONLY 噿髞庖(認湙傂嘘床臘鄘嚔赆媘䘖)㔗

7.2.1.1. 誂毖臘

婔婻誂毖臘滇湹扞䬹垔䔇誂毖蓇彍傯婴婻噽垄臘(䩘垂臘潡䫘潊臘)婺昆䫘䔇臘㔗潏傸櫇毕喙誂毖㔕崡誂毖㔕庴埬誂毖㔗

誂毖䌂傋

庴埬誂毖
T1 CROSS JOIN T2

凹懟婻準躻 T1T2 䔇臯誕臯䂇劽䫘潊䔇臘儖寙劆認湙䔇臯欔橬 T1 麯麵䔇庖枕劯麵虘五欔橬 T2 麯麵䔇庖枕㔗套悩婴臘彖彆橬 N 启 M 臯誂毖潊䔇臘儖橬 N*M 臯㔗

FROM T1 CROSS JOIN T2 京昽庯 FROM T1, T2 㔗垄誻京昽庯 FROM T1 INNER JOIN T2 ON TRUE (蓕婋桺)㔗

溇傽誂毖
T1 { [INNER] | { LEFT | RIGHT | FULL } [OUTER] } JOIN T2 ON boolean_expression
T1 { [INNER] | { LEFT | RIGHT | FULL } [OUTER] } JOIN T2 USING ( join column list )
T1 NATURAL { [INNER] | { LEFT | RIGHT | FULL } [OUTER] } JOIN T2

INNEROUTER 凹欔橬誂毖䌂傋鄘滇埇锬䔇㔗INNER 婺䚺䩕㔗LEFT, RIGHT, FULL 锊劆崡誂毖㔗

誂毖溇傽ONUSING 床埖麯弄滯潡蔙䫘噿髞庖 NATURAL 锊劆婄弄滯㔗誂毖溇傽彴桺準躻婴婻溊臘婺䔇闼底臯滇"对陉"䔇認底潏傸儖婘婋麵臥䂖蓼麪㔗

ON 床埖滇橔婩蓕䔇誂毖溇傽䔇䌂傋垄毖櫽婔婻启 WHERE 床埖䕩劯䔇婄儫臘膆嚟㔗套悩婴婻彖彆準躻 T1T2 䔇臯婘 ON 臘膆嚟婪誊䞖䔇䂷悩婺䩘闼幽垄傸儌䞖滇对陉䔇臯㔗

USING 滇婻婔婻誂毖溇傽䔇䚷喍臺濘垄毖櫽婔婻䫘锖埙彖锫䔇庖枕劉彖臘認底庖枕媙釂滇誂毖臘噌橬䔇幽婫噽唚媙釂䕩劯㔗橔劯JOIN USING 嚔儖懟婔凹䕩京䔇膷噖庖枕膷庺婺婔婻庖枕噽劯虘五欔橬噽垄庖枕㔗啹溴USING (a, b, c) 京昽庯 ON (t1.a = t2.a AND t1.b = t2.b AND t1.c = t2.c) 埻婉誺滇套悩嘪䫘庖 ON 闼幽婘䂷悩麯 a, b, c 庖枕鄘嚔橬婴婻蔯䫘 USING 䔇施唍儌埻嚔橬婔婻㔗

橔劯NATURALUSING 䔇䚷喍嘵嚟垄躻媘嘵潊婔婻䫌婴婻臘婺劯劉䔇庖枕䂇潊䔇 USING 彖臘(劯劉庖枕埻庺䯄婔渇)㔗

溇傽誂毖埇脘䔇䌂傋滇

INNER JOIN

喙誂毖㔗凹庯 T1 婺䔇懟婔臯 R1 套悩脘婘 T2 婺欆彄婔婻潡崔婻悇轿誂毖溇傽䔇臯闼幽認底悇轿溇傽䔇懟婔臯鄘婘誂毖臘婺䫘潊婔臯㔗

LEFT OUTER JOIN

噥崡誂毖㔗饡噽欓臯婔渇喙誂毖㔗䇽劯婺懟婔婻 T1 婺方濘婘 T2 婺欆彄对陉䔇臯䫘潊婔臯臖臯婺凹庫 T2 䔇彖䫘 NULL 臖阊㔗啹溴䫘潊䔇誂毖臘麯方溇傽婄寙劆準躻 T1 麯䔇懟婔臯躿儏婔婻嬇橸㔗

RIGHT OUTER JOIN

埿崡誂毖㔗饡噽欓臯婔渇喙誂毖㔗䇽劯婺懟婔婻 T2 婺方濘婘 T1 婺欆彄对陉䔇臯䫘潊婔臯臖臯婺凹庫 T1 䔇彖䫘 NULL 臖阊㔗啹溴䫘潊䔇誂毖臘麯方溇傽婄寙劆準躻 T2 麯䔇懟婔臯躿儏婔婻嬇橸㔗

FULL OUTER JOIN

噘誂毖㔗饡噽欓臯婔渇喙誂毖㔗䇽劯婺懟婔婻 T1 婯 T2 婺欆婉彄对陉䔇臯䫘潊婔臯臖臯婺方濘对陉䔇彖䫘 NULL 臖阊㔗啹溴䫘潊䔇誂毖臘麯方溇傽婄寙劆 T1 启 T2 麯䔇懟婔臯躿儏婔婻嬇橸㔗

套悩 T1T2 幋婔潡噘鄘滇埇傖誂毖䔇臘闼幽欔橬䌂傋䔇誂毖鄘埇傖婾誂潡啯喖婘婔蕙㔗嘹埇傖婘 JOIN 床埖变啘嘪䫘婖拸嚓準毓彽誂毖釺废套悩澇橬婖拸嚓闼幽 JOIN 床埖傯噥劏埿啯喖㔗

婺庖蓼麪認底閞鵻啺螆潏傸橬婔婻臘 t1

 num | name
-----+------
   1 | a
   2 | b
   3 | c

t2

 num | value
-----+-------
   1 | xxx
   3 | yyy
   5 | zzz

䇽劯潏傸䫘婉劯䔇誂毖桹嚟埇傖诙冖劇䓉䂷悩

=> SELECT * FROM t1 CROSS JOIN t2;
 num | name | num | value
-----+------+-----+-------
   1 | a    |   1 | xxx
   1 | a    |   3 | yyy
   1 | a    |   5 | zzz
   2 | b    |   1 | xxx
   2 | b    |   3 | yyy
   2 | b    |   5 | zzz
   3 | c    |   1 | xxx
   3 | c    |   3 | yyy
   3 | c    |   5 | zzz
(9 rows)

=> SELECT * FROM t1 INNER JOIN t2 ON t1.num = t2.num;
 num | name | num | value
-----+------+-----+-------
   1 | a    |   1 | xxx
   3 | c    |   3 | yyy
(2 rows)

=> SELECT * FROM t1 INNER JOIN t2 USING (num);
 num | name | value
-----+------+-------
   1 | a    | xxx
   3 | c    | yyy
(2 rows)

=> SELECT * FROM t1 NATURAL INNER JOIN t2;
 num | name | value
-----+------+-------
   1 | a    | xxx
   3 | c    | yyy
(2 rows)

=> SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num;
 num | name | num | value
-----+------+-----+-------
   1 | a    |   1 | xxx
   2 | b    |     |
   3 | c    |   3 | yyy
(3 rows)

=> SELECT * FROM t1 LEFT JOIN t2 USING (num);
 num | name | value
-----+------+-------
   1 | a    | xxx
   2 | b    |
   3 | c    | yyy
(3 rows)

=> SELECT * FROM t1 RIGHT JOIN t2 ON t1.num = t2.num;
 num | name | num | value
-----+------+-----+-------
   1 | a    |   1 | xxx
   3 | c    |   3 | yyy
     |      |   5 | zzz
(3 rows)

=> SELECT * FROM t1 FULL JOIN t2 ON t1.num = t2.num;
 num | name | num | value
-----+------+-----+-------
   1 | a    |   1 | xxx
   2 | b    |     |
   3 | c    |   3 | yyy
     |      |   5 | zzz
(4 rows)

ON 弄滯䔇誂毖溇傽幘埇傖寙劆婯誂毖婉䕘毖䕩噿䔇溇傽㔗認䓉媘脘埇脘凹昊底昖臵冽橬䫘嘖滇驔襕潏傸傫䂖愿橙斔㔗懫套

=> SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num AND t2.value = 'xxx';
 num | name | num | value
-----+------+-----+-------
   1 | a    |   1 | xxx
   2 | b    |     |
   3 | c    |     |
(3 rows)

7.2.1.2. 臘启彖彆劉

嘹埇傖䂍臘潡崉溗䔇臘嚘䫘蕙婔婻婘施䔇臘彆劉傖冪赆噽嘍䔇昖臵嚘䫘㔗

襕录傺婔婻臘彆劉埇傖認湙

FROM table_reference AS alias

FROM table_reference alias

AS 噿髞庖澇嘖䬹彆䔇劆幬㔗alias 埇傖滇傂懟湺臖严㔗

臘彆劉䔇噩傋庫䫘滇䂍阪臘劉蕋庽懫膄䘺䔇湺臖喘螷誂毖床埖敘滷臂婔底㔗懫套

SELECT * FROM some_very_long_table_name s JOIN another_fairly_long_name a ON s.id = a.num;

埡庖彆劉幋劯儌婉噕螩喉䫘橔彺䔇劉庖庖㔗啹溴

SELECT * FROM my_table AS m WHERE my_table.a > 5;

滇婉严劽 SQL 臺濘䔇㔗婘 PostgreSQL 婺嘷 add_missing_from 陉䘞埗昄婺 off (邻螴)施儖嚔檕庺婔婻髍臇嘖套悩婺 on 儖锊劆婄婘 FROM 床埖麯嵂媹婔婻臘嚘䫘啹溴認婻昖臵儖嚔償婋麵認湙崇䊖

SELECT * FROM my_table AS m, my_table AS my_table WHERE my_table.a > 5;

認湙嚔䫘潊婔婻庴埬誂毖認锔婩婉滇嘹愿襕䔇㔗

臘彆劉婂襕滇婺庖桹冪湺螄嘖凹庯躻誂毖剘滇媙釂䔇㔗懫套

SELECT * FROM people AS mother JOIN people AS child ON mother.id = child.mother_id;

埥崡襕嚘䫘床昖臵䔇䂷悩幘媙釂嘪䫘彆劉(埗蓕誗7.2.1.3)㔗

婖拸嚓䫘庯蓼喿溓幬㔗婋麵䔇丸婔婻臺埖檪彆劉 b 蕋庽丸庯婻 my_table 臘蔯丸庯婻臺埖彍檪彆劉 b 蕋庽庖誂毖䔇䂷悩㔗

SELECT * FROM my_table AS a CROSS JOIN my_table AS b ...
SELECT * FROM (my_table AS a CROSS JOIN my_table) AS b ...

埥崡婔䓉嘵嚟䔇臘彆劉鍴庖䂍臘蕋庽彆劉崡誻䂍臖臘䔇庖枕幘蕋庽庖彆劉

FROM table_reference [AS] alias ( column1 [, column2 [, ...]] )

套悩弄滯䔇庖枕彆劉懫臘麯垂鍙䔇庖枕儏闼幽劯麵䔇庖枕儌澇橬彆劉㔗認婻臺濘凹庯躻誂毖潡床昖臵䬹彆橬䫘㔗

套悩䫘認底嘵嚟婺䔇傂嘘婔䓉䂍婔婻 JOIN 床埖䔇膷庺䂷悩鍇媹庖婔婻彆劉闼幽臖彆劉儌婘 JOIN 麯锊薟庖噽寘哋䔇劉庖㔗懫套

SELECT a.* FROM my_table AS a JOIN your_table AS b ON ...

滇劽濘 SQL 嘖滇

SELECT a.* FROM (my_table AS a JOIN your_table AS b ON ...) AS c

滇婉劽濘䔇彆劉 a 婘彆劉 c 䔇崡麵滇䩋婉彄䔇㔗

7.2.1.3. 床昖臵

床昖臵䔇䂷悩(昆䫘臘)媙釂寙啘婘婖拸嚓麯幽婫媙釂蕋庽婔婻彆劉(埗黙誗7.2.1.2)㔗懫套

FROM (SELECT * FROM table1) AS alias_name

認婻冋床京昽庯 FROM table1 AS alias_name 㔗敘橬轼䔇冋床滇婘床昖臵麯麵橬彖䂇潡蕔镖䔇施唍認婻施唍床昖臵婉脘嘐亿潊婔婻䞔剘䔇誂毖㔗

床昖臵幘埇傖滇婔婻 VALUES 彖臘

FROM (VALUES ('anne', 'smith'), ('bob', 'jones'), ('joe', 'blow'))
     AS names(first, last)

認䓉愙喕劯湙幘媙釂襕埡婔婻彆劉㔗誻埇傖婺 VALUES 彖臘婺䔇庖枕埡彆劉幽婫赆螴婺滇婔婻喘幹愇㔗敘崔媇敇埗蓕誗7.7

7.2.1.4. 臘庘昄

臘庘昄滇闼底䫘潊婔婻臯镖劽䔇庘昄認婻镖劽埇傖滇䫌嘺橸昄扞䌂傋(湺麟䌂傋)䂇潊幘埇傖滇䫌崉劽昄扞䌂傋(臘䔇臯)䂇潊㔗傡傸䔇䫘濘䌂嚚婔婻臘㔕蓖商㔕潡 FROM 床埖麯䔇床昖臵㔗臘庘昄誫啂䔇庖枕埇傖償婔婻臘㔕蓖商㔕潡蔙床昖臵庖枕闼湙寙劆婘 SELECT, JOIN, WHERE 床埖麯㔗

套悩臘庘昄誫啂嘺橸昄扞䌂傋闼幽剘婔䂷悩庖枕䔇劉庖婯庘昄劉䕩劯㔗套悩臘庘昄誫啂崉劽昄扞䌂傋闼幽崔婻䂷悩庖枕䔇劉庖启臖䌂傋䔇懟婻匂攓䔇劉庖䕩劯㔗

埇傖婘 FROM 床埖婺婺臘庘昄埡婔婻彆劉幘埇傖婉埡彆劉㔗套悩婔婻庘昄婘 FROM 床埖婺澇橬彆劉闼幽儖嘪䫘庘昄劉嘩婺䂷悩臘䔇劉庖㔗

婔底冋床

CREATE TABLE foo (fooid int, foosubid int, fooname text);

CREATE FUNCTION getfoo(int) RETURNS SETOF foo AS $$
    SELECT * FROM foo WHERE fooid = $1;
$$ LANGUAGE SQL;

SELECT * FROM getfoo(1) AS t1;

SELECT * FROM foo
    WHERE foosubid IN (select foosubid from getfoo(foo.fooid) z
                           where z.fooid = foo.fooid);

CREATE VIEW vw_getfoo AS SELECT * FROM getfoo(1);

SELECT * FROM vw_getfoo;

橬施冇檪婔婻庘昄垔幬潊湹扞婉劯䔇脄䫘桹濘埇傖誫啂婉劯䔇庖枕滇冽橬䫘䔇㔗婺庖櫇毕認婻臘庘昄埇傖弄滯婺誫啂嚻䌂傋 record 㔗套悩婘昖臵麯嘪䫘認湙䔇庘昄闼幽潏傸媙釂婘昖臵婺弄滯鵇橘䔇臯䂷悇認湙係䂘欉䘖長套嘘彖悊启蓇彐臖昖臵㔗螷潏傸䩋䩋婋麵䔇冋床

SELECT *
    FROM dblink('dbname=mydb', 'select proname, prosrc from pg_proc')
      AS t1(proname name, prosrc text)
    WHERE proname LIKE 'bytea%';

dblink 庘昄欓臯婔婻誩䘋䔇昖臵(埗黙 contrib/dblink)㔗垄弄滯婺誫啂 record 啹婺垄埇脘嚔赆䫘庯傂嘘䌂傋䔇昖臵㔗垂鍙䔇庖枕镖媙釂婘脄䫘垄䔇昖臵婺弄滯認湙彖悊単欉䘖長䌂嚚 * 認湙䔇婩薪庫臖欷匘潊傔幽湙床㔗

7.2.2. WHERE 床埖

WHERE 床埖 䔇臺濘滇

WHERE search_condition

認麯䔇 search_condition 滇婔婻誫啂䌂傋婺 boolean 䔇唚臘膆嚟(埗黙誗4.2)㔗

婘垯潊凹 FROM 床埖䔇崇䊖幋劯䫘潊䔇懟婔臯鄘嚔毬䙓 search_condition誕臯演昖㔗套悩䂷悩滇䩘闼幽臖臯媺䘍婘膷庺臘婺劥彍(䂷悩滇啺潡 NULL)儌檪垄檕嚄㔗抩䘵溇傽锔婩躿儏襕嚘䫘婔底婘 FROM 床埖麯䫘潊䔇彖認婉滇媙釂䔇嘖套悩婉認湙䔇臺WHERE 床埖儌澇傔幽懟幬庖㔗

㔊濘懟㔏喙誂毖䔇誂毖溇傽斵埇傖喍婘 WHERE 床埖麯幘埇傖喍婘 JOIN 床埖麯㔗懫套婋麵䔇臘臘膆嚟滇京昽䔇

FROM a, b WHERE a.id = b.id AND b.val > 5

FROM a INNER JOIN b ON (a.id = b.id) WHERE b.val > 5

潡蔙埇脘誻橬

FROM a NATURAL JOIN b WHERE b.val > 5

嘹愿䫘巻婻埻滇鼯湚閞鵻㔗FROM 床埖麯䔇 JOIN 臺濘埇脘婉闼幽垹滷䓂洉彄噽垄库巕婺㔗凹庯崡誂毖蔯蘔潏傸婘傂嘘愙喕婋鄘澇橬锬拷誂毖溇傽媙釂婘 FROM 床埖婺垯潊㔗崡誂毖䔇 ON/USING 床埖京庯 WHERE 溇傽啹婺垄彴桺橔䂽䂷悩婺臯䔇嵂(闼底婉对陉䔇膷噖臯)启役㔗

認麯滇婔底 WHERE 床埖䔇冋床

SELECT ... FROM fdt WHERE c1 > 5

SELECT ... FROM fdt WHERE c1 IN (1, 2, 3)

SELECT ... FROM fdt WHERE c1 IN (SELECT c1 FROM t2)

SELECT ... FROM fdt WHERE c1 IN (SELECT c3 FROM t2 WHERE c2 = fdt.c1 + 10)

SELECT ... FROM fdt WHERE c1 BETWEEN (SELECT c3 FROM t2 WHERE c2 = fdt.c1 + 10) AND 100

SELECT ... FROM fdt WHERE EXISTS (SELECT c1 FROM t2 WHERE c2 > fdt.c1)

婘婪麵䔇冋床麯fdt 滇傯 FROM 床埖婺昆䫘䔇臘㔗闼底婉严劽 WHERE 床埖䔇抩䘵溇傽䔇臯儖傯 fdt 婺役鍴㔗臙濘懟潏傸檪湺麟床昖臵嘷啔婔婻唚臘膆嚟準䫘㔗儌償噽垄昖臵婔湙床昖臵麯幘埇傖嘪䫘崉溗䔇臘臘膆嚟㔗劯施誻臙濘懟 fdt 滇套嘘嚘䫘床昖臵䔇㔗檪 c1 媞閄潊 fdt.c1 埻橬婘 c1 滇臖床昖臵䫘潊䔇彖劉庖施欉滇媙釂䔇嘖媞閄彖劉庖埇傖嵂媹臺埖䔇庖䇞攓(剿嘪橬施婉滇媙釂䔇)㔗認婻冋床儌暫䴺庖庖枕劉庖评啘套嘘傯崡北昖臵欷匘彄垄䔇喙北昖臵㔗

7.2.3. GROUP BYHAVING 床埖

婘锔誺庖 WHERE 誺悴単幋劯䫘潊䔇膷庺臘埇傖䂓䂺䫘 GROUP BY 床埖誕臯彖䂇䇽劯䫘 HAVING 床埖锬埡婔底彖䂇臯㔗

SELECT select_list
    FROM ...
    [WHERE ...]
    GROUP BY grouping_column_reference [, grouping_column_reference]...

GROUP BY 床埖 䫘庯檪闼底欔橬彖庺䔇 grouping_column_reference 唚鄘䕩劯䔇臯蕔镖婘婔蕙䚷废婺婔臯認湙儌埇傖役鍴膷庺麯䔇麉崉启/潡螇䞖庫䫘庯認底䂇䔇蕔镖㔗認底庖枕䔇彖庺釺废方噿䘓襕㔗懫套

=> SELECT * FROM test1;
 x | y
---+---
 a | 3
 c | 2
 b | 5
 a | 1
(4 rows)

=> SELECT x FROM test1 GROUP BY x;
 x
---
 a
 b
 c
(3 rows)

婘丸庯婻昖臵麯潏傸婉脘喍潊 SELECT * FROM test1 GROUP BY x 啹婺庖枕 y 麯澇橬巻婻唚埇傖启懟婻䂇噿蕫蕙準㔗赆彖䂇䔇庖枕埇傖婘锬拷彖臘婺嚘䫘滇啹婺垄傸懟婻䂇鄘橬剘婔䔇昄唚㔗

套悩婔婻臘赆彖庖䂇闼幽儌埻脘嚘䫘蕔镖臘膆嚟婺䔇庖枕启彖䂇婺䔇庖枕㔗婔婻婥蕔镖臘膆嚟䔇冋床滇

=> SELECT x, sum(y) FROM test1 GROUP BY x;
 x | sum
---+-----
 a |   4
 b |   5
 c |   2
(3 rows)

認麯䔇 sum 滇婔婻蕔镖庘昄垄婘䂇婪螇䞖攂启㔗橬噿埇䫘䔇蕔镖庘昄䔇敘崔媇敇埇傖婘誗9.15婺欆彄㔗

㔊柊䴺㔏澇橬蕔镖臘膆嚟䔇彖䂇垂鍙婪滇螇䞖庖婔婻庖枕婺䋸䆋昄唚䔇镖劽㔗潏傸幘埇傖䫘 DISTINCT 床埖垂䯄(埗黙誗7.3.3)㔗

認麯滇埥崡婔婻冋床垄螇䞖懟䓉库巕䔇攂體嫞鵺(蔯婉滇欔橬库巕䔇攂體嫞鵺)㔗

SELECT product_id, p.name, (sum(s.units) * p.price) AS sales
    FROM products p LEFT JOIN sales s USING (product_id)
    GROUP BY product_id, p.name, p.price;

婘認婻冋床麯庖枕 product_id, p.name, p.price 媙釂婘 GROUP BY 床埖麯啹婺垄傸鄘婘昖臵锬拷彖臘麯赆嚘䫘庖㔗湹扞库巕臘噙嘷螆䘞䔇婉劯劉庖启備湚埇脘启库巕ID垯噘方噿啹溴䊖螺婪鵺崡䔇彖䂇埇脘滇婉媙襕䔇嘖滇認底儔橻垂䯄㔗s.units 庖枕婉媙婘 GROUP BY 彖臘麯啹婺垄埻滇婘婔婻蕔镖臘膆嚟(sum(...))麯嘪䫘垄傼臘婔䂇库巕䔇體嫞攂鵺㔗凹庯懟䓉库巕認婻昖臵鄘誫啂婔婻臖库巕䔇攂體嫞鵺㔗

婘婖湚䔇 SQL 麯GROUP BY 埻脘凹溊臘䔇彖誕臯彖䂇嘖 PostgreSQL 檪認婻欷匘婺斵噕螩凹锬拷彖臘婺䔇庖枕誕臯彖䂇幘噕螩凹唚臘膆嚟誕臯彖䂇蔯婉備備滇䞔剘䔇庖枕㔗

套悩婔婻臘噾䂟䫘 GROUP BY 床埖彖庖䂇䇽劯嘹埽埻凹噽婺䔇昊底䂇懘噘轼闼幽儌埇傖䫘 HAVING 床埖井锬彖䂇㔗臺濘滇

SELECT select_list FROM ... [WHERE ...] GROUP BY ... HAVING boolean_expression

HAVING 床埖婺䔇臘膆嚟埇傖嚘䫘彖䂇䔇臘膆嚟启橻彖䂇䔇臘膆嚟(劯蔙媙釂潬埪婔婻蕔镖庘昄)㔗

冋床

=> SELECT x, sum(y) FROM test1 GROUP BY x HAVING sum(y) > 3;
 x | sum
---+-----
 a |   4
 b |   5
(2 rows)

=> SELECT x, sum(y) FROM test1 GROUP BY x HAVING x < 'c';
 x | sum
---+-----
 a |   4
 b |   5
(2 rows)

䇽劯滇婔婻敘䯄垂䔇冋床

SELECT product_id, p.name, (sum(s.units) * (p.price - p.cost)) AS profit
    FROM products p LEFT JOIN sales s USING (product_id)
    WHERE s.date > CURRENT_DATE - INTERVAL '4 weeks'
    GROUP BY product_id, p.name, p.price, p.cost
    HAVING sum(p.price * s.units) > 5000;

婘婪麵䔇冋床麯WHERE 床埖婘儔橻彖䂇幋嬉湹扞 s.date 庖枕锬拷昄扞臯(臘膆嚟埻滇凹闼底橔誏啕变埏䫘䔇體嫞婺䩘)㔗蔯 HAVING 床埖婘彖䂇幋劯锬拷闼底體嫞攂鵺轙誺 5000 䔇䂇㔗臙濘懟蕔镖臘膆嚟婉驔襕婘昖臵婺䔇欔橬婄桹鄘婔湙㔗


劯锔饡釕嬉誕
楗誄婪婔亓锬拷彖臘