W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
表達式是一個或者多個值、運算符和 SQL 函數(shù)的組合。每個表達式都有值,通過求值可以得到。
SQL 表達式看起來就像數(shù)學公式一樣,它們以查詢語言寫就。你也可以用它們在數(shù)據(jù)庫中查詢符合特定條件的數(shù)據(jù)。
考慮如下所示的 SELECT 語句的基本語法:
SELECT column1, column2, columnN
FROM table_name
WHERE [CONDITION|EXPRESSION];
SQL 表達式有很多種不同的類型,如下所示:
SQL 布爾表達式以單值條件檢索數(shù)據(jù),其語法如下:
SELECT column1, column2, columnN
FROM table_name
WHERE SINGLE VALUE MATCHTING EXPRESSION;
考慮如下所示的客戶信息表:
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)
數(shù)值表達式用于在查詢中執(zhí)行數(shù)學運算。其語法如下:
SELECT numerical_expression as OPERATION_NAME
[FROM table_name
WHERE CONDITION] ;
這里 numerical_expression 為數(shù)學運算或者任何公式。下面是一個說明 SQL 數(shù)值表達式用法的簡單例子:
SQL> SELECT (15 + 6) AS ADDITION
+----------+
| ADDITION |
+----------+
| 21 |
+----------+
1 row in set (0.00 sec)
SQL 有一系列的內(nèi)建函數(shù),例如 avg()、sum()、count() 等,這些函數(shù)用于在表上或者表中的特定列上執(zhí)行聚合數(shù)據(jù)運算。
SQL> SELECT COUNT(*) AS "RECORDS" FROM CUSTOMERS;
+---------+
| RECORDS |
+---------+
| 7 |
+---------+
1 row in set (0.00 sec)
時間表達式返回當前系統(tǒng)的日期和時間:
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)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: