表达式是一个或多个值,运算符和SQL函数计算结果的值的组合。
SQL表达式与公式相似,他们编写查询语言。您也可以使用它们来查询数据库中的特定的数据集。
考虑SELECT语句的基本语法如下:
SELECT column1, column2, columnN
FROM table_name
WHERE [CONDITION|EXPRESSION];
有不同类型的SQL表达式,其中可提到如下面:
SQL布尔表达式提取匹配单个值为基础的数据。下面是语法:
SELECT column1, column2, columnN
FROM table_name
WHERE SINGLE VALUE MATCHTING EXPRESSION;
考虑CUSTOMERS表具有以下记录:
SQL> SELECT * FROM CUSTOMERS;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
7 rows in set (0.00 sec)
下面是简单的例子,显示SQL布尔表达式的用法:
SQL> SELECT * FROM CUSTOMERS WHERE SALARY = 10000;
+----+-------+-----+---------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+-------+-----+---------+----------+
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+-------+-----+---------+----------+
1 row in set (0.00 sec)
这个表达式用来在任何查询执行任何数学运算。下面是语法:
SELECT numerical_expression as OPERATION_NAME
[FROM table_name
WHERE CONDITION] ;
这里数值表达式被用于数学表达式或任何公式。 下面是一个简单的例子,显示SQL数字表达式的用法:
SQL> SELECT (15 + 6) AS ADDITION
+----------+
| ADDITION |
+----------+
| 21 |
+----------+
1 row in set (0.00 sec)
还有像一些内置函数avg(), sum(), count()等,执行了被称为数据计算对表或特定的表列集合体。
SQL> SELECT COUNT(*) AS "RECORDS" FROM CUSTOMERS;
+---------+
| RECORDS |
+---------+
| 7 |
+---------+
1 row in set (0.00 sec)
日期表达式返回当前系统日期和时间值:
SQL> SELECT CURRENT_TIMESTAMP;
+---------------------+
| Current_Timestamp |
+---------------------+
| 2009-11-12 06:40:23 |
+---------------------+
1 row in set (0.00 sec)
另一个日期表达式如下:
SQL> SELECT GETDATE();;
+-------------------------+
| GETDATE |
+-------------------------+
| 2009-10-22 12:07:18.140 |
+-------------------------+
1 row in set (0.00 sec)