考慮SELECT語句的基本語法如下:
SELECT column1, column2, columnN FROM table_name WHERE [CONTION | EXPRESSION];
PostgreSQL表達式,這是下面提到的不同類型的:
PostgreSQL的布爾表達式匹配的單個值的基礎上取數據。以下是語法:
SELECT column1, column2, columnN FROM table_name WHERE SINGLE VALUE MATCHTING EXPRESSION;
考慮表 COMPANY有如下記錄:
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)
下面是一個簡單的例子顯示使用PostgreSQL的布爾表達式:
testdb=# SELECT * FROM COMPANY WHERE SALARY = 10000;
以上PostgreSQL的語句會產生以下結果:
id | name | age | address | salary ----+-------+-----+----------+-------- 7 | James | 24 | Houston | 10000 (1 row)
這些表達式用來執行任何數學運算的任何查詢。以下是語法:
SELECT numerical_expression as OPERATION_NAME [FROM table_name WHERE CONDITION] ;
這裡數值表達式是用於數學表達式或任何公式。下面是一個簡單的例子顯示使用SQL數值表達式:
testdb=# SELECT (15 + 6) AS ADDITION ;
以上PostgreSQL表達式會產生以下結果:
addition ---------- 21 (1 row)
There are several built-in functions like avg(), sum(), count() etc to perform what is known as aggregate data calculations against a table or a specific table column.
testdb=# SELECT COUNT(*) AS "RECORDS" FROM COMPANY;
以上PostgreSQL的表會產生以下結果:
RECORDS --------- 7 (1 row)
日期表達式返回當前系統日期和時間值,這些表達式將被用於各種數據操作。
testdb=# SELECT CURRENT_TIMESTAMP;
以上PostgreSQL的表會產生以下結果:
now ------------------------------- 2013-05-06 14:38:28.078+05:30 (1 row)