SQL表達式
表達式是一個或多個值,運算符和SQL函數計算結果的值的組合。
SQL表達式與公式相似,他們編寫查詢語言。您也可以使用它們來查詢數據庫中的特定的數據集。
語法
考慮SELECT語句的基本語法如下:
SELECT column1, column2, columnN FROM table_name WHERE [CONDITION|EXPRESSION];
有不同類型的SQL表達式,其中可提到如下麵:
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)
SQL - 數字表達式:
這個表達式用來在任何查詢執行任何數學運算。下麵是語法:
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 - 日期表達式:
日期表達式返回當前係統日期和時間值:
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)