PostgreSQL䔇WHERE床埖䫘庯毺垔溇傽蔯诙埡䔇昄扞埇傖婘剘婻臘潡崔婻臘媹噖㔗
套悩䂍垔䔇溇傽悇轿欉誫啂傯臘婺䔇噙嘷昄唚㔗幘埇傖嘪䫘WHERE床埖誺悴毬婉婯橕庺䯄䔇䂷悩镖臯㔗 www.gitbook.net
WHERE床埖婉備滇婘SELECT臺埖婺嘪䫘嘖垄幘埇䫘庯婘UPDATEDELETE臺埖京潏傸嚔婘傖劯䔇䆹誗螾蓼㔗 www.gitbook.net
WHERE床埖SELECT臺埖䔇嘺橸臺濘套婋
SELECT column1, column2, columnN FROM table_name WHERE [search_condition]gitbook.net
埇傖毺垔嘪䫘䔇search_condition懫膄潡锂膏誊䞖严㔗套><=LIKE㔗 www.gitbook.net
蔄荏臘橬套婋螄嘘 www.gitbook.net
testdb# select * from COMPANY; id | name | age | address | salary ----+-------+-----+-----------+-------- 1 | Paul | 32 | California| 20000 2 | Allen | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 6 | Kim | 22 | South-Hall| 45000 7 | James | 24 | Houston | 10000 (7 rows) gitbook.net
婋麵滇䞔剘䔇冋床滆䴺PostgreSQL䔇锂膏誊䞖严䔇䫘濘㔗婋麵䔇SELECT臺埖嚔彖庺欔橬幘醇崓庯潡京庯25幽婫噖蕇滇崓庯潡京庯65000.00螄嘘 gitbook.net
testdb=# SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;gitbook.net
傖婪PostgreSQL䔇臘嚔库䫘傖婋䂷悩
id | name | age | address | salary ----+-------+-----+------------+-------- 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 (2 rows)gitbook.net
婋麵䔇SELECT臺埖彖庺幘醇崓庯潡京庯25潡衻愘崓庯潡京庯65000.00欔橬䔇螄嘘彖臘 gitbook.net
testdb=# SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000; www.gitbook.net
傖婪PostgreSQL䔇臘嚔库䫘傖婋䂷悩 gitbook.net
id | name | age | address | salary ----+-------+-----+-------------+-------- 1 | Paul | 32 | California | 20000 2 | Allen | 25 | Texas | 15000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 (4 rows) www.gitbook.net
婋麵䔇SELECT臺埖彖臘幘醇婉婺NULL認懟叿五欔橬螄嘘婋準䔇欔橬螄嘘啹婺螄嘘滇澇橬幘醇京庯NULL gitbook.net
testdb=# SELECT * FROM COMPANY WHERE AGE IS NOT NULL;gitbook.net
傖婪PostgreSQL䔇臘嚔库䫘傖婋䂷悩 www.gitbook.net
id | name | age | address | salary ----+-------+-----+------------+-------- 1 | Paul | 32 | California | 20000 2 | Allen | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 6 | Kim | 22 | South-Hall | 45000 7 | James | 24 | Houston | 10000 (7 rows)www.gitbook.net
婋麵䔇SELECT臺埖彖臘劉䓄傖'Pa'嚔哋䔇欔橬䔇螄嘘, 嘖滇澇橬傂嘘对陉'Pa'䔇螄嘘. gitbook.net
testdb=# SELECT * FROM COMPANY WHERE NAME LIKE 'Pa%';gitbook.net
傖婪PostgreSQL䔇臘嚔库䫘傖婋䂷悩 www.gitbook.net
id | name | age |address | salary ----+------+-----+-----------+-------- 1 | Paul | 32 | California| 20000gitbook.net
Following SELECT statement list down all the records where AGE value is either 25 or 27: gitbook.net
testdb=# SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 ); www.gitbook.net
Above PostgreSQL statement will produce following result:
id | name | age | address | salary ----+-------+-----+------------+-------- 2 | Allen | 25 | Texas | 15000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 (3 rows)www.gitbook.net
婋麵䔇SELECT臺埖彖臘庖欔橬䔇幘醇唚滇25潡27䔇螄嘘 www.gitbook.net
testdb=# SELECT * FROM COMPANY WHERE AGE NOT IN ( 25, 27 );gitbook.net
傖婪PostgreSQL䔇臘嚔库䫘傖婋䂷悩
gitbook.net
id | name | age | address | salary ----+-------+-----+------------+-------- 1 | Paul | 32 | California | 20000 3 | Teddy | 23 | Norway | 20000 6 | Kim | 22 | South-Hall | 45000 7 | James | 24 | Houston | 10000 (4 rows)gitbook.net
婋麵䔇SELECT臺埖彖臘唚滇幘醇婘25启27幋閘䔇欔橬螄嘘 www.gitbook.net
testdb=# SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27; gitbook.net
傖婪PostgreSQL䔇臘嚔库䫘傖婋䂷悩 gitbook.net
id | name | age | address | salary ----+-------+-----+------------+-------- 2 | Allen | 25 | Texas | 15000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 (3 rows) gitbook.net
婋麵䔇SELECT臺埖嘪䫘SQL床昖臵床昖臵埏䯄欔橬螄嘘幘醇庖枕衻麏>65000䇽劯嘪䫘WHERE床埖启嘪䫘EXISTS婔蕙淉嘩彖庺庖欔橬䔇螄嘘庻婘幘醇傯崡麵昖臵床昖臵誫啂䔇䂷悩
www.gitbook.net
testdb=# SELECT AGE FROM COMPANY WHERE EXISTS (SELECT AGE FROM COMPANY WHERE SALARY > 65000);www.gitbook.net
傖婪PostgreSQL䔇臘嚔库䫘傖婋䂷悩
gitbook.net
age ----- 32 25 23 25 27 22 24 (7 rows) www.gitbook.net
婋麵䔇SELECT臺埖嘪䫘SQL床昖臵床昖臵昖欆欔橬螄嘘幘醇庖枕婺橬噖蕇>65000劯┋┋嘪䫘WHERE床埖>誊䞖严婔蕙彖庺庖欔橬䔇螄嘘噽婺傯崡鄘昖臵幘醇崓庯幘醇婘床昖臵誫啂䔇䂷悩 www.gitbook.net
testdb=# SELECT * FROM COMPANY WHERE AGE > (SELECT AGE FROM COMPANY WHERE SALARY > 65000); www.gitbook.net
傖婪PostgreSQL䔇臘嚔库䫘傖婋䂷悩 gitbook.net
id | name | age | address | salary ----+------+-----+------------+-------- 1 | Paul | 32 | California | 20000www.gitbook.net