位置:首頁 > 數據庫 > SQL教學 > SQL SELECT查詢語句

SQL SELECT查詢語句

SQL SELECT語句用來從數據庫表獲取數據返回表形式數據的結果。這些結果表被稱為結果集。

語法

SELECT語句的基本語法如下:

SELECT column1, column2, columnN FROM table_name;

這裡, column1, column2...是要獲取其值的表的字段。 如果你想獲取所有可用字段,那麼可以使用下麵的語法:

SELECT * FROM table_name;

例子:

考慮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 |
+----+----------+-----+-----------+----------+

下麵是一個例子,這將獲取CUSTOMERS表中可用的客戶的ID, Name 和 Salary:

SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;

這將產生以下結果:

+----+----------+----------+
| ID | NAME     | SALARY   |
+----+----------+----------+
|  1 | Ramesh   |  2000.00 |
|  2 | Khilan   |  1500.00 |
|  3 | kaushik  |  2000.00 |
|  4 | Chaitali |  6500.00 |
|  5 | Hardik   |  8500.00 |
|  6 | Komal    |  4500.00 |
|  7 | Muffy    | 10000.00 |
+----+----------+----------+

如果你想獲取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 |
+----+----------+-----+-----------+----------+