接著前面的寫 選擇特殊的列 舉個慄子:我想給這些pet們過身日,我就要知道pet們的名字和他們的生日。我該怎麼辦? 操作如下: SELECT name, birth FROM pet; mysql> SELECT name, birth -> FROM pet;+ + +| name | birth ...
接著前面的寫
選擇特殊的列
舉個慄子:我想給這些pet們過身日,我就要知道pet們的名字和他們的生日。我該怎麼辦?
操作如下:
SELECT name, birth FROM pet;
mysql> SELECT name, birth
-> FROM pet;
+----------+------------+
| name | birth |
+----------+------------+
| Whistler | 1997-12-09 |
| Puffball | 1999-03-30 |
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Buffy | 1989-05-13 |
| Fang | 1990-08-27 |
| Bowser | 1989-08-31 |
| Chirpy | 1998-09-11 |
| Slim | 1996-04-29 |
+----------+------------+
9 rows in set (0.00 sec)
mysql>
這樣我就可以給這些pet們過生日了。
再舉一個慄子:沒有故事,我就是想看一看這些小天使們的主人都有誰?
操作如下:
SELECT owner FROM pet;
mysql> SELECT owner
-> FROM pet;
+--------+
| owner |
+--------+
| Gwen |
| Diane |
| Harold |
| Gwen |
| Harold |
| Benny |
| Diane |
| Gwen |
| Benny |
+--------+
9 rows in set (0.00 sec)
mysql>
you會發現you查詢到的 owner 中有好多是重覆的誒,這好煩啊。
為了讓他們值出現一次 ,加一個關鍵字 DISTINCT
SELECT DISTINCT owner FROM pet;
mysql> SELECT DISTINCT owner
-> FROM pet;
+--------+
| owner |
+--------+
| Gwen |
| Diane |
| Harold |
| Benny |
+--------+
4 rows in set (0.00 sec)
mysql>
這下子看著舒服多了
再帶條件舉一個慄子:要想查詢狗和貓的出生日期
操作如下:
SELECT name, species, birth FROM pet WHERE species = 'dog' OR species = 'cat';
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 |
+--------+---------+------------+
5 rows in set (0.00 sec)
mysql>
分類行
年輕人,你可能註意到了,前面的慄子中的結果,是沒有排好序的。這要是給你的老阪看的話,是要挨批的(雖然你愚蠢的老阪可能看不懂)。所以,就有了ORDER BY 這個關鍵字
舉個慄子:給pet們的出生日期排序。 不要問為什麼,無聊。
操作如下:
SELECT name, birth FROM pet ORDER BY birth;
mysql> SELECT name, birth
-> FROM pet
-> ORDER BY birth;
+----------+------------+
| name | birth |
+----------+------------+
| Buffy | 1989-05-13 |
| Bowser | 1989-08-31 |
| Fang | 1990-08-27 |
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Slim | 1996-04-29 |
| Whistler | 1997-12-09 |
| Chirpy | 1998-09-11 |
| Puffball | 1999-03-30 |
+----------+------------+
9 rows in set (0.18 sec)
mysql>
這樣看起來就better了呢(然並卵)。
順便多一句嘴,有一個ORDER BY BINARY col_name 自己百度自己查。
預設的排序是升序,最小的值在第一行。
if(想要以降序排列){
在排序的列名上增加DESC(降序)關鍵字
}
操作如下:
SELECT name, birth FROM pet ORDER BY birth DESC;
mysql> SELECT name, birth
-> FROM pet
-> ORDER BY birth DESC;
+----------+------------+
| name | birth |
+----------+------------+
| Puffball | 1999-03-30 |
| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
| Claws | 1994-03-17 |
| Fluffy | 1993-02-04 |
| Fang | 1990-08-27 |
| Bowser | 1989-08-31 |
| Buffy | 1989-05-13 |
+----------+------------+
9 rows in set (0.00 sec)
mysql>
還可以對多個列進行排序, 並且可以按不同的方向對不同的列進行排序。(書上寫的好複雜啊)
舉個例子:按升序對動物的種類進行排序,然後按降序根據生日對各動物種類進行排序(最年輕的動物在最前面)
操作如下:
SELECT name, species, birth FROM pet ORDER BY species, birth DESC;
mysql> SELECT name, species, birth
-> FROM pet
-> ORDER BY species, birth DESC;
+----------+---------+------------+
| name | species | birth |
+----------+---------+------------+
| Chirpy | bird | 1998-09-11 |
| Whistler | bird | 1997-12-09 |
| Claws | cat | 1994-03-17 |
| Fluffy | cat | 1993-02-04 |
| Fang | dog | 1990-08-27 |
| Bowser | dog | 1989-08-31 |
| Buffy | dog | 1989-05-13 |
| Puffball | hamster | 1999-03-30 |
| Slim | snake | 1996-04-29 |
+----------+---------+------------+
9 rows in set (0.08 sec)
mysql>
我的理解:二級排序,升序排動物五中,降序排年齡。
註意:(書真是為了你們操碎了心)DESC 關鍵字, 隻影響了birth, 不影響species。
先寫到這裡吧。ヾ(≧O≦)〃嗷~