考虑SELECT语句的基本语法如下: gitbook.net
SELECT column1, column2, columnN FROM table_name WHERE [CONTION | EXPRESSION];gitbook.net
PostgreSQL表达式,这是下面提到的不同类型的: gitbook.net
PostgreSQL的布尔表达式匹配的单个值的基础上取数据。以下是语法:
SELECT column1, column2, columnN FROM table_name WHERE SINGLE VALUE MATCHTING EXPRESSION;www.gitbook.net
考虑表 COMPANY有如下记录: 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的布尔表达式: gitbook.net
testdb=# SELECT * FROM COMPANY WHERE SALARY = 10000; gitbook.net
以上PostgreSQL的语句会产生以下结果:
id | name | age | address | salary ----+-------+-----+----------+-------- 7 | James | 24 | Houston | 10000 (1 row)www.gitbook.net
这些表达式用来执行任何数学运算的任何查询。以下是语法: gitbook.net
SELECT numerical_expression as OPERATION_NAME [FROM table_name WHERE CONDITION] ;gitbook.net
这里数值表达式是用于数学表达式或任何公式。下面是一个简单的例子显示使用SQL数值表达式:
testdb=# SELECT (15 + 6) AS ADDITION ; gitbook.net
以上PostgreSQL表达式会产生以下结果:
addition ---------- 21 (1 row) www.gitbook.net
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;www.gitbook.net
以上PostgreSQL的表会产生以下结果: gitbook.net
RECORDS --------- 7 (1 row) gitbook.net
日期表达式返回当前系统日期和时间值,这些表达式将被用于各种数据操作。 www.gitbook.net
testdb=# SELECT CURRENT_TIMESTAMP; www.gitbook.net
以上PostgreSQL的表会产生以下结果: gitbook.net
now
-------------------------------
2013-05-06 14:38:28.078+05:30
(1 row) gitbook.net