发布于 2015-07-12 03:04:51 | 273 次阅读 | 评论: 0 | 来源: 网络整理
考虑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)