📅  最后修改于: 2020-12-29 04:31:18             🧑  作者: Mango
表达式是一个或多个值,运算符以及对一个值求值的SQL函数的组合。这些SQL EXPRESSION类似于公式,它们以查询语言编写。您还可以使用它们来查询数据库以获取一组特定的数据。
考虑一下SELECT语句的基本语法,如下所示:
SELECT column1, column2, columnN
FROM table_name
WHERE [CONDITION|EXPRESSION];
有不同类型的SQL表达式,如下所述-
现在让我们详细讨论每个。
SQL布尔表达式基于匹配单个值来获取数据。以下是语法-
SELECT column1, column2, columnN
FROM table_name
WHERE SINGLE VALUE MATCHING 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] ;
此处,numerical_expression用于数学表达式或任何公式。以下是显示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)