唚臘膆嚟䫘婘劇䓉臺濘䯇嵄婺懫套婘 SELECT 变傴䔇䕞湺彖臘婺婘 INSERT 潡 UPDATE 婺䫘嘩桄䔇彖唚潡蔙婘螩崔变傴䔇抩䘵溇傽婺嘪䫘㔗潏傸橬施唍檪唚臘膆嚟䔇䂷悩埆啔湺麟 傖冪婯婔婻臘臘膆嚟䔇䂷悩䕩寺彆(滇婔婻臘)㔗啹溴唚臘膆嚟幘埆啔湺麟臘膆嚟(潡䞔䓄臘膆嚟)㔗臘膆嚟臺濘噕螩凹準躻嘺橸鄘彖䔇昄唚誕臯䞖橇㔕锂膏㔕镖劽㔕启噽垄誊䞖㔗
唚臘膆嚟滇婋彖喙垹幋婔
婔婻婩麟潡蔙庖麵唚
婔婻庖枕嚘䫘
婔婻嘉䘞埗昄嚘䫘(婘庘昄弄滯嘷婺潡鵇䚡喍䔇臺埖婺)
婔婻婋湺臘膆嚟
婔婻庖枕锬拷臘膆嚟
婔婻淉嘩严脄䫘
婔婻庘昄脄䫘
婔婻蕔镖臘膆嚟
婔婻䌂傋蘸扵
婔婻湺麟床昖臵
婔婻昄䂇悇锹単
婔婻臯悇锹単
鍴庖認婻彖臘傖崡誻橬螩崔悇锹埇傖嘐䌂婺臘膆嚟嘖滇婉镕冻傂嘘锔䫘䔇臺濘蓇彍㔗垄傸锔婩橬庘昄潡淉嘩严䔇臺幬幽婫婘䆹9麯劽锗䔇嘉䘞柟誄㔗婔婻冋床滇 IS NULL 床埖㔗
潏傸噾䂟婘誗4.1.2麯橬螘螺誺䔇喙垹庖㔗婋麵䔇誗螘螺嬷婋䔇锬釹㔗
婔婻庖枕埇傖䫘婋麵䔇嘵嚟嚘䫘
correlation.columnname
correlation 滇婔婻臘䔇劉庖(埇脘橬昇嚟媞閄)潡蔙滇䫘 FROM 床埖認湙䔇桹濘垔幬䔇臘䔇彆劉潡蔙滇噿髞庖 NEW 潡 OLD (NEW 启 OLD 埻脘庺䯄婘婔溇櫹喍蓇彍婺蔯噽垄䕩噿䔇劉庖埇傖䫘庯傂懟 SQL 臺埖婺)㔗套悩婘嘷嬉昖臵欔嘪䫘䔇欔橬臘婺臖庖枕劉庖滇嫇婔䔇闼幽認婻䕩噿劉庖(correlation)启彖锫䫘䔇䗹儌埇傖䩕䘖(埗蓕䆹7)㔗
嘉䘞埗昄嚘䫘䫘庯湺臖傯崡鄘䂍 SQL 臺埖䔇埗昄㔗埗昄䫘庯 SQL 庘昄垔幬臺埖启鵇䚡喍䔇昖臵㔗橬底垵潙䆇康誻櫇毕婘 SQL 变傴庖严婾崡膹弄滯昄扞唚認䓉愙喕婋埗昄䫘庯嚘䫘 SQL 庖严婾臯崡䔇昄扞㔗婔婻埗昄䔇嘵嚟套婋
$number
懫套婋麵認婻 dept
庘昄䔇垔幬
CREATE FUNCTION dept(text) RETURNS dept AS $$ SELECT * FROM dept WHERE name = $1 $$ LANGUAGE SQL;
婘庘昄赆脄䫘䔇施唍認麯䔇 $1 儖嚘䫘丸婔婻埗昄㔗
套悩婔婻臘膆嚟䫘潊婔婻昄䂇䌂傋䔇昄唚闼幽潏傸埇傖锔誺婋麵認湙䔇臘膆嚟準柊埡昄䂇婺䔇噄䘹
expression[subscript]
套悩滇崔婻䕩闂䔇噄䘹("昄䂇䬺桺")埇傖䫘婋麵䔇桹濘檘埡([ ] 毬䙓庖麵桺橸䔇桹嚟庺䯄)
expression[lower_subscript:upper_subscript]
懟婻 subscript 躻噌鄘滇婔婻臘膆嚟垄媙釂䫘潊婔婻昘昄唚㔗
锔婩昄䂇 expression媙釂䫘婖拸嚓寙啘嘖套悩埻滇婔婻庖枕嚘䫘潡蔙婔婻嘉䘞埗昄闼幽婖拸嚓埇傖䩕䘖㔗劯湙套悩溊昄䂇滇崔䂘䔇闼幽崔婻婋湺埇傖誂毖婘婔蕙㔗懫套
mytable.arraycolumn[4] mytable.two_d_column[17][34] $1[10:42] (arrayfunction(a,b))[42]
橔劯婔婻冋床麯䔇婖拸嚓滇媙釂䔇㔗埗黙誗8.10诙埡橬噿昄䂇䔇敘崔媇敇㔗
套悩婔婻臘膆嚟䫘潊婔婻崉劽䌂傋(臯䌂傋)闼幽䫘婋麵䔇桹濘埇傖檘埡婔婻毺垔䔇庖枕
expression.fieldname
锔婩臯 expression 媙釂䫘婖拸嚓寙啘嘖滇套悩襕锬埡䔇臘膆嚟埻滇婔婻臘嚘䫘潡蔙嘉䘞埗昄埇傖䩕䘖婖拸嚓㔗懫套
mytable.mycolumn $1.somecolumn (rowfunction(a,b)).col3
啹溴婔婻噘䓄䔇庖枕嚘䫘垂鍙婪埻滇婔婻庖枕锬拷臺濘䔇䬹冋㔗
淉嘩严脄䫘橬婬䓉臺濘
expression operator expression (埯䕞婺䚔淉嘩严) |
operator expression (剘䕞嬉䚔淉嘩严) |
expression operator (剘䕞劯䚔淉嘩严) |
OPERATOR(schema.operatorname)
噙嘷庻婘巻婻淉嘩严傖埪垄傸滇剘䕞誻滇埯䕞埡喿庯係䂘潡䫘潙垔幬庖傔幽淉嘩严㔗䆹9柟誄庖喙䘞䔇淉嘩严㔗
庘昄脄䫘䔇臺濘滇劽濘庘昄劉(埇脘橬昇嚟劉媞閄)劯麵虘五寙劆埗昄彖臘䔇婖拸嚓
function ([expression [, expression ...]] )
懫套婋麵䔇傼乕螇䞖 2 䔇广桹湹
sqrt(2)
喙䘞庘昄䔇彖臘婘䆹9麯㔗噽垄庘昄埇䫌䫘潙時媹㔗
婔婻蕔镖臘膆嚟傼臘婔婻蕔镖庘昄凹昖臵锬庺䔇臯䔇崇䊖㔗婔婻蕔镖庘昄檪崔婻膷噖䚷废婺婔婻膷庺唚懫套䂍膷噖挗启潡挗广庺㔗婔婻蕔镖臘膆嚟䔇臺濘滇婋彖幋婔
aggregate_name (expression [ , ... ] ) aggregate_name (ALL expression [ , ... ] ) aggregate_name (DISTINCT expression [ , ... ] ) aggregate_name ( * )
認麯䔇 aggregate_name 滇嬉麵垔幬䔇蕔镖(埇脘滇婥橬昇嚟䔇噘䓄)蔯 expression 滇婔婻橸躆婉寙劆蕔镖臘膆嚟䔇傂懟唚臘膆嚟㔗
丸婔䓉嘵嚟䔇蕔镖臘膆嚟婺欔橬麂 NULL 䔇膷噖臯脄䫘蕔镖(垂鍙婪滇劥媘䘖 NULL 䫌蕔镖庘昄喿垔嘖滇欔橬湺庖䔇蕔镖庘昄鄘媘䘖垄傸)㔗丸庯䓉嘵嚟婯丸婔䓉京備(啹婺 ALL 滇䚺䩕唚)㔗丸婬䓉嘵嚟婺欔橬膷噖臯婺欔橬嫇婔䔇麂 NULL 唚脄䫘蕔镖㔗橔劯婔䓉嘵嚟婺懟婻膷噖臯(婉䞇滇劥婺 NULL)脄䫘婔渇蕔镖啹婺澇橬弄滯䬹垔䔇膷噖唚㔗锔婩垄埻䫘庯 count(*)
蕔镖庘昄㔗
懫套count(*) 䫘潊膷噖臯䔇攂昄count(f1) 䫘潊 f1 婉婺 NULL 䔇膷噖臯昄count(distinct f1) 䫘潊 f1 嫇婔婫麂 NULL 䔇臯昄㔗
鵇垔幬䔇蕔镖庘昄婘誗9.15麯柟誄㔗噽垄蕔镖庘昄埇傖䫌䫘潙嵂媹㔗
婔婻蕔镖臘膆嚟埻脘婘SELECT 变傴䔇䂷悩彖臘潡蔙 HAVING 床埖麯庺䯄㔗䥕溵婘噽垄床埖麯庺䯄(懫套 WHERE 床埖)啹婺認底床埖锂膏婪婘䫘潊蕔镖䂷悩幋嬉螇䞖㔗
套悩婔婻蕔镖臘膆嚟庺䯄婘婔婻床昖臵麯(埗黙誗4.2.9启誗9.16)蕔镖锔婩滇婘床昖臵婺誕臯螇䞖㔗嘖滇套悩蕔镖䔇埗昄埻寙劆崡北昖臵䔇埻麟彍冋崡認婻蕔镖嚔匂庯䥂傡橔誏䔇崡北昖臵幽婫婘臖昖臵婪誕臯螇䞖㔗臖蕔镖臘膆嚟昘嘷婪匂庯垄庺䯄䔇床昖臵凹崡北昖臵䔇嚘䫘噽嘩䫘䕩嘷庯床昖臵懟婔渇螇䞖婺䔇婔婻婩麟㔗嬉誄鍊彽(蕔镖臘膆嚟埻脘庺䯄婘䂷悩彖潡蔙 HAVING 床埖婺)埻锗䫘庯蕔镖欔匂䔇昖臵北㔗
㔊濘懟㔏PostgreSQL 䕞嬉幽婉櫇毕婥橬崔婻膷噖臘膆嚟䔇 DISTINCT 㔗
婔婻䌂傋蘸扵弄滯婔婻傯婔䓉昄扞䌂傋彄埥崡婔䓉昄扞䌂傋䔇蘸扵㔗PostgreSQL 毖埖婴䓉京昽䔇䌂傋蘸扵臺濘
CAST ( expression AS type ) expression::type
CAST 臺濘镕冻 SQL 湺庖:: 臺濘滇 PostgreSQL 寖埾䫘濘㔗
套悩凹婔婻噾䘖䌂傋䔇唚臘膆嚟庫䫘蘸扵垄傼臘婔婻誊臯施䌂傋蘸扵㔗埻橬婘噾䂟垔幬庖劽锗䔇䌂傋蘸扵淉嘩䔇愙喕婋臖蘸扵欉脘潊媘㔗臙濘懟認婔䗹启䫘庯婩麟䔇蘸扵䘖橬寺彆(套誗4.1.2.5欔䴺)㔗婔婻庫䫘庯庖严婾桺橸䔇蘸扵臘䴺䂍臖庖严婾桺橸䔇唚蕋庽婔婻彺哋䌂傋啹溴垄凹庯傂嘘䌂傋鄘嚔潊媘(套悩庖严婾桺橸䔇喙垹严劽臖昄扞䌂傋䔇膷噖臺濘)㔗
套悩婔婻唚臘膆嚟䔇唚凹昊䌂傋蔯蘔婉庻婘晙晖䔇愙喕闼幽潏傸埇傖䩕䘖滯䇞䔇䌂傋蘸扵(懫套婘䂍婔婻臘庖枕蕋唚䔇施唍)蔯䫌係䂘躻媘欓臯䌂傋蘸扵㔗婉誺躻媘蘸扵埻锗䫘庯闼底係䂘臘婺湺螄五"OK to apply implicitly"䔇蘸扵庘昄㔗噽垄蘸扵庘昄媙釂䫘滯䇞䔇蘸扵臺濘脄䫘㔗認底鍊彽滇婺庖镪噉婔底攻嚗䔇蘸扵赆躻媘䔇庫䫘㔗
潏傸幘埇傖䫘庘昄鼯湚䔇臺濘弄滯婔婻䌂傋蘸扵
typename ( expression )
婉誺認婻桹濘埻脘䫘庯闼底䌂傋劉劯施幘滇橬昽庘昄劉䔇䌂傋㔗懫套double precision 儌婉脘認幽䫘嘖滇京昽䔇 float8 埇傖㔗劯湙interval, time, timestamp 套悩媹庖埯嚘埙幘埻脘認幽䫘啹婺庻婘臺濘喾仕㔗啹溴庘昄鼯湚䔇䌂傋蘸扵嚔凚躘婉婔躘欔傖庫臖镪噉認幽嘪䫘㔗庘昄湙臺濘垂鍙婪儌滇婔婻庘昄脄䫘㔗套悩嘪䫘婴䓉湺庖蘸扵臺濘啔誊臯施蘸扵闼幽垄儖婘喙鄘脄䫘婔婻噾濘喯䔇庘昄欓臯蘸扵㔗锔婩認䓉蘸扵庘昄启垄傸䔇膷庺䌂傋劯劉嘖滇埇傖䓂洉䔇䘋废婉脘冺蕡認婔䗹㔗
婔婻湺麟床昖臵滇婔婻櫆婘婖拸嚓麯埻誫啂婔臯婔彖䔇捞锔 SELECT 昖臵(埗黙䆹7诙埡橬噿幥喍昖臵䔇媇敇)㔗臖 SELECT 儖赆欓臯蔯噽誫啂唚儖婘变啘䔇唚臘膆嚟婺嘪䫘㔗檪婔婻誫啂轙誺婔臯潡蔙轙誺婔彖䔇昖臵䫘啔湺麟昖臵滇髍臇䔇㔗婉誺床昖臵婉誫啂臯彍婉䞖髍臇(湺麟䂷悩赆螴婺滇 NULL)㔗床昖臵埇傖嚘䫘崡啘昖臵䔇埻麟認底埻麟婘懟渇床昖臵婺嘷啔婩麟嘪䫘㔗埗蓕誗9.16傖诙埡噽垄寙劆床昖臵䔇臘膆嚟㔗
懫套婋麵䔇昖臵欆庺懟婻噂婺䔇橔崓庺埼昄麟䔇嘯婗
SELECT name, (SELECT max(pop) FROM cities WHERE cities.state = states.name) FROM states;
婔婻昄䂇悇锹単滇婔婻臘膆嚟垄傯躻躆潊叻噄䘹婪悇锹婔婻昄䂇唚㔗婔婻䞔剘䔇昄䂇悇锹単䫌噿髞庖 ARRAY 㔕婔婻噥桹拸嚓 [ 㔕婔婻潡崔婻臘䴺昄䂇噄䘹唚䔇臘膆嚟(䫘锖埙彖锫)㔕婔婻埿桹拸嚓 ] 䂇潊㔗懫套
SELECT ARRAY[1,2,3+4]; array --------- {1,2,7} (1 row)
昄䂇噄䘹䌂傋滇潊叻臘膆嚟䔇噸噌䌂傋嘪䫘启 UNION 潡 CASE悇锹婔湙䔇蓇彍喿垔(埗黙誗10.5)㔗
崔䂘昄䂇唚埇傖锔誺啯喖昄䂇悇锹単䔇桹濘準彽嘩㔗喙北悇锹単婺䔇 ARRAY 噿髞庖埇傖䩕䘖㔗懫套婋麵䔇婴埖䫘潊劯湙䔇䂷悩
SELECT ARRAY[ARRAY[1,2], ARRAY[3,4]]; array --------------- {{1,2},{3,4}} (1 row) SELECT ARRAY[[1,2],[3,4]]; array --------------- {{1,2},{3,4}} (1 row)
啹婺崔䂘昄䂇媙釂滇桹嘵欔傖劯北䔇喙北悇锹単媙釂䫘潊劯䂘䔇床昄䂇㔗
崔䂘昄䂇悇锹単噄䘹埇傖滇傂嘘䫘潊劽锗昄䂇䔇婩薪蔯婉備備滇婔婻床 ARRAY 悇锹㔗懫套
CREATE TABLE arr(f1 int[], f2 int[]); INSERT INTO arr VALUES (ARRAY[[1,2],[3,4]], ARRAY[[5,6],[7,8]]); SELECT ARRAY[f1, f2, '{{9,10},{11,12}}'::int[]] FROM arr; array ------------------------------------------------ {{{1,2},{3,4}},{{5,6},{7,8}},{{9,10},{11,12}}} (1 row)
潏傸幘埇傖傯婔婻床昖臵䔇䂷悩婺悇锹婔婻昄䂇㔗溴施昄䂇悇锹単滇噿髞庖 ARRAY 劯虘五婔婻䫘婖拸嚓(婉滇桹拸嚓)寙啘䔇床昖臵㔗懫套
SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%'); ?column? ------------------------------------------------------------- {2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31} (1 row)
床昖臵媙釂埻誫啂婔婻剘䋸䔇庖枕㔗䫘潊䔇婔䂘昄䂇儖婺床昖臵麯懟臯䂷悩䫘潊婔婻噄䘹噄䘹䌂傋对陉床昖臵䔇膷庺庖枕㔗
䫘 ARRAY 傺䆋䔇昄䂇婋湺攂滇傯弹嚔哋㔗橬噿昄䂇䔇敘崔媇敇埗黙誗8.10㔗
臯悇锹単滇婔婻傯柊冕䂍垄䔇潊叻庖枕昄唚婺悇锹臯唚(幘埆崉劽䌂傋唚)䔇臘膆嚟㔗婔婻臯悇锹単䫌噿髞庖 ROW 㔕婔婻噥婖拸嚓㔕镽婻潡崔婻嘩婺臯庖枕唚䔇臘膆嚟(䫘锖埙彖锫)㔕婔婻埿婖拸嚓䂇潊㔗懫套
SELECT ROW(1,2.5,'this is a test');
套悩婘彖臘麯橬崔婻臘膆嚟闼幽噿髞庖 ROW 滇埇锬䔇㔗
臯悇锹単埇傖寙劆 rowvalue.* 臺濘垄儖赆欷匘婺臯唚噄䘹䔇彖臘儌償儖 .* 臺濘䫘庯婔婻 SELECT 彖臘釽北婔湙㔗冋套套悩臘 t 橬 f1 启 f2 婴婻庖枕闼幽婋麵婴埖滇京備䔇
SELECT ROW(t.*, 42) FROM t; SELECT ROW(t.f1, t.f2, 42) FROM t;
㔊濘懟㔏婘 PostgreSQL 8.2幋嬉.* 臺濘滇婉嚔赆欷匘䔇欔傖 ROW(t.*, 42) 儖录傺婔婻婴庖枕䔇臯噽丸婔婻庖枕滇埥婔臯䔇唚㔗桄䔇臯婺锔婩敘橬䫘㔗套悩嘹驔襕斓嚟䔇啯喖臯唚䔇啔濘臙儖喙鄘䔇臯唚喍潊婉寙劆 .* 懫套 ROW(t, 42) 㔗
䚺䩕施ROW 臘膆嚟录傺䔇唚滇婔婻寪劉䔇螄嘘䌂傋㔗套悩媙襕嘹埇傖檪垄蘸扵潊婔婻变劉䔇崉劽䌂傋(斵埇傖滇婔婻臘䔇臯䌂傋幘埇傖滇婔婻䫘 CREATE TYPE AS 录傺䔇崉劽䌂傋)㔗埇脘嚔驔襕婔婻滯䇞䔇蘸扵傖镪噉溓幬㔗懫套
CREATE TABLE mytable(f1 int, f2 float, f3 text); CREATE FUNCTION getf1(mytable) RETURNS int AS 'SELECT $1.f1' LANGUAGE SQL; -- 啹婺埻橬婔婻getf1()庻婘欔傖婉驔襕䌂傋蘸扵 SELECT getf1(ROW(1,2.5,'this is a test')); getf1 ------- 1 (1 row) CREATE TYPE myrowtype AS (f1 int, f2 text, f3 numeric); CREATE FUNCTION getf1(myrowtype) RETURNS int AS 'SELECT $1.f1' LANGUAGE SQL; -- 䯄婘潏傸驔襕䌂傋蘸扵傖臘滯脄䫘巻婻庘昄 SELECT getf1(ROW(1,2.5,'this is a test')); ERROR: function getf1(record) is not unique SELECT getf1(ROW(1,2.5,'this is a test')::mytable); getf1 ------- 1 (1 row) SELECT getf1(CAST(ROW(11,'this is a test',2.5) AS myrowtype)); getf1 ------- 11 (1 row)
臯悇锹単埇傖䫘庯彽嘩庻嗘婘崉劽䌂傋庖枕婺䔇崉劽䌂傋唚潡蔙滇嚹锐䂍婔婻毖埖崉劽䌂傋埗昄䔇庘昄㔗埥崡潏傸幘埇傖䫘垄懫膄婴婻臯唚潡蔙䫘 IS NULL 潡 IS NOT NULL 敋臘婔婻臯唚懫套
SELECT ROW(1,2.5,'this is a test') = ROW(1, 3, 'not the same'); SELECT ROW(table.*) IS NULL FROM table; -- detect all-null rows
床臘膆嚟䔇螇䞖釺废滇澇橬垔幬䔇㔗䬹彆襕毺庺䔇滇婔婻淉嘩严潡蔙庘昄䔇膷噖幽婉婔垔滇毬䙓傯噥劏埿䔇釺废潡蔙傖昊䓉䬹垔䔇釺废誕臯螇䞖䔇㔗
埥崡套悩婔婻臘膆嚟䔇䂷悩埇傖锔誺埻彴桺垄䔇婔鄘彖儌埇傖冖彄闼幽噽垄床臘膆嚟儌埇傖垯噘婉螇䞖庖㔗懫套套悩潏傸認幽喍
SELECT true OR somefunc();
闼幽 somefunc() 儌(埇脘)湹橸婉嚔赆脄䫘㔗剿嘪償婋麵認湙喍幘滇婔湙
SELECT somefunc() OR true;
臙濘懟認启昊底䚡䘋臺蘔麯傯噥劏埿"䘺虇"滇婉婔湙䔇㔗
啹溴拪橬嬇嘩䫘䔇庘昄嘩婺崉溗臘膆嚟䔇婔鄘彖滇婉滯捺䔇㔗婘 WHERE 启 HAVING 床埖麯冺蕡嬇嘩䫘潡蔙滇螇䞖釺废滇䬹彆剌鍷䔇啹婺認底床埖鄘滇嘩婺䫘潊婔婻欓臯蓇彐䔇婔鄘彖誕臯庖崓麟䔇喉崇䊖㔗婘認底床埖麯䔇婄儫臘膆嚟(AND/OR/NOT 䔇䂇劽)埇傖䫘婄儫傼昄誊䞖冋噕螩䔇傂嘘桹嚟誕臯臖彆㔗
套悩驔襕嚺彽螇䞖釺废闼幽埇傖嘪䫘 CASE 悇锹(埗黙誗9.13)㔗懫套婋麵滇婔䓉嚕商镪噉婘 WHERE 床埖麯赆镽鍴䔇婉埇麹桹濘
SELECT ... WHERE x <> 0 AND y/x > 1.5;
嘖滇婋麵認婻滇垬噘䔇
SELECT ... WHERE CASE WHEN x <> 0 THEN y/x > 1.5 ELSE false END;
認䓉鼯湚䔇 CASE 悇锹嚔黂溵嚻寡啹溴庫臖埻婘媙襕䔇施唍欉嘪䫘㔗婘認婻䬹枪䔇冋床麯懆方䡏閞喍潊 y > 1.5*x 敘喘㔗