䯄婘嘉䘞饡釕 > 昄扞康 > PostgreSQL > PostgreSQL WHERE臺埖

PostgreSQL WHERE臺埖

準溊寘录桺䆹    䫌 悕垵幥 敘桄䬽橸    敟蓽庺渇

PostgreSQL䔇WHERE床埖䫘庯毺垔溇傽蔯诙埡䔇昄扞埇傖婘剘婻臘潡崔婻臘媹噖㔗

www.gitbook.net

套悩䂍垔䔇溇傽悇轿欉誫啂傯臘婺䔇噙嘷昄唚㔗幘埇傖嘪䫘WHERE床埖誺悴毬婉婯橕庺䯄䔇䂷悩镖臯㔗 www.gitbook.net

WHERE床埖婉備滇婘SELECT臺埖婺嘪䫘嘖垄幘埇䫘庯婘UPDATEDELETE臺埖京潏傸嚔婘傖劯䔇䆹誗螾蓼㔗 www.gitbook.net

臺濘

WHERE床埖SELECT臺埖䔇嘺橸臺濘套婋

gitbook.net

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䔇臘嚔库䫘傖婋䂷悩

www.gitbook.net

 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|  20000
 
gitbook.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:

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)
 

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 |  20000 
www.gitbook.net
橸䆍桺䆹鍴濘滯蘸蘘崡庺婺橸䆍寘录潡䚡臏
渵誯傂嘘嘵嚟䔇蘸蘘嘖臙媇媙濘滯庺崇優麉傡庺媿媘嚹携庥幹嘺䇔昍䘋
蘸蘘臙濘滯桺䆹蘸蘘躻悕垵幥 [http://www.gitbook.net]
橸桺湺鵻PostgreSQL WHERE臺埖
蘸蘘臙媺䘍寘桺鷆毖:http://www.gitbook.net/html/postgresql/2013/080557.html
婪婔䇺PostgreSQL臘膆嚟      婋婔䇺PostgreSQL AND/OR誊䞖严