MySQL™ 參考手冊(從表中檢索信息)

從表中檢索信息

SELECT語句用於從表中提取信息,語句的通常形式是:mysql

SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy;

what_to_select表示你想要看到的內容,這能夠是列列表,或*表示「全部列」。which_table表示要從中檢索數據的表,WHERE子句是可選的,若是存在,則conditions_to_satisfy指定行必須知足的一個或多個條件纔有資格進行檢索。sql

查詢全部數據

最簡單的SELECT形式從表中檢索全部內容:數據庫

mysql> SELECT * FROM pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
| Puffball | Diane  | hamster | f    | 1999-03-30 | NULL       |
+----------+--------+---------+------+------------+------------+

若是要查看整個表,例如在剛剛加載初始數據集以後,這種形式的SELECT很是有用。例如,你可能會認爲Bowser的出生日期彷佛不太合適,諮詢你原來的資料,你會發現正確的出生年應該是1989年,而不是1979年。segmentfault

至少有兩種方法能夠解決這個問題:測試

  • 編輯文件pet.txt以更正錯誤,而後使用DELETELOAD DATA清空表並從新加載它:code

    mysql> DELETE FROM pet;
    mysql> LOAD DATA LOCAL INFILE 'pet.txt' INTO TABLE pet;

    可是,若是這樣作,你還必須從新輸入Puffball的記錄。ci

  • 使用UPDATE語句僅修復錯誤記錄:字符串

    mysql> UPDATE pet SET birth = '1989-08-31' WHERE name = 'Bowser';

    UPDATE僅更改有問題的記錄,不須要你從新加載表。get

查詢特定行

如上一節所示,能夠輕鬆檢索整個表,只需省略SELECT語句中的WHERE子句便可,但一般你不但願看到整個表,特別是當它變大時。相反,你一般對回答特定問題更感興趣,在這種狀況下,你能夠對所需信息指定一些約束,讓咱們看看一些選擇查詢,它們會回答關於你的寵物的問題。it

你只能從表中選擇特定行,例如,若是你想驗證你對Bowser出生日期所作的更改,請選擇Bowser的記錄,以下所示:

mysql> SELECT * FROM pet WHERE name = 'Bowser';
+--------+-------+---------+------+------------+------------+
| name   | owner | species | sex  | birth      | death      |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+

輸出確認年份被正確記錄爲1989年,而不是1979年。

字符串比較一般不區分大小寫,所以你能夠將name指定爲'bowser''BOWSER'等等,查詢結果是同樣的。

你能夠在任何列上指定條件,而不單單是name,例如,若是你想知道1998年或以後出生的動物,請測試birth列:

mysql> SELECT * FROM pet WHERE birth >= '1998-1-1';
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |
| Puffball | Diane | hamster | f    | 1999-03-30 | NULL  |
+----------+-------+---------+------+------------+-------+

例如,你能夠結合條件來定位雌性狗:

mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

前面的查詢使用AND邏輯運算符,還有一個OR運算符:

mysql> SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL  |
| Slim     | Benny | snake   | m    | 1996-04-29 | NULL  |
+----------+-------+---------+------+------------+-------+

ANDOR能夠混合,但AND的優先級高於OR,若是你同時使用這兩個運算符,最好使用括號明確指出條件應如何分組:

mysql> SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')
       OR (species = 'dog' AND sex = 'f');
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

查詢特定列

若是你不想查看錶中的整行,只需命名你感興趣的列,使用逗號分隔。例如,若是你想知道你的動物什麼時候出生,請選擇namebirth列:

mysql> SELECT name, birth FROM pet;
+----------+------------+
| name     | birth      |
+----------+------------+
| Fluffy   | 1993-02-04 |
| Claws    | 1994-03-17 |
| Buffy    | 1989-05-13 |
| Fang     | 1990-08-27 |
| Bowser   | 1989-08-31 |
| Chirpy   | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim     | 1996-04-29 |
| Puffball | 1999-03-30 |
+----------+------------+

要找出誰擁有寵物,請使用此查詢:

mysql> SELECT owner FROM pet;
+--------+
| owner  |
+--------+
| Harold |
| Gwen   |
| Harold |
| Benny  |
| Diane  |
| Gwen   |
| Gwen   |
| Benny  |
| Diane  |
+--------+

請注意,查詢只是從每條記錄中檢索owner列,其中一些列出現不止一次,要最小化輸出,請經過添加關鍵字DISTINCT檢索每一個惟一的輸出記錄一次:

mysql> SELECT DISTINCT owner FROM pet;
+--------+
| owner  |
+--------+
| Benny  |
| Diane  |
| Gwen   |
| Harold |
+--------+

你可使用WHERE子句將行選擇與列選擇組合在一塊兒,例如,要僅獲取狗和貓的出生日期,請使用如下查詢:

mysql> SELECT name, species, birth FROM pet
       WHERE species = 'dog' OR species = 'cat';
+--------+---------+------------+
| name   | species | birth      |
+--------+---------+------------+
| Fluffy | cat     | 1993-02-04 |
| Claws  | cat     | 1994-03-17 |
| Buffy  | dog     | 1989-05-13 |
| Fang   | dog     | 1990-08-27 |
| Bowser | dog     | 1989-08-31 |
+--------+---------+------------+

上一篇:建立和使用數據庫

相關文章
相關標籤/搜索