SELECT 語句

2018-08-12 22:09 更新

SQL SELECT 語句

SQL SELECT 語句用于從數(shù)據(jù)庫的表中取回所需的數(shù)據(jù),并以表的形式返回。返回的表被稱作結(jié)果集。

語法:

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;

運行結(jié)果如下所示:

    +----+----------+----------+
    | 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 查詢應(yīng)該這么寫:

    SQL> SELECT * FROM CUSTOMERS;

運行結(jié)果如下所示:

    +----+----------+-----+-----------+----------+
    | 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 |
    +----+----------+-----+-----------+----------+
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號