重拾SQL——表中索值

2016.10.23 + 2016.11.02css

1.選擇全部數據(查看整表)

MariaDB [tianyuan]> 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  |
| Puffball | Diane  | hamster | f    | 1999-03-30 | NULL  |
+----------+--------+---------+------+------------+-------+
4 rows in set (0.00 sec)

2.修改數據的二種辦法

2.1 直接單處更新(優)

MariaDB [tianyuan]> UPDATE pet SET birth = '1994-04-12' WHERE name = 'Buffy';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
MariaDB [tianyuan]> 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    | 1994-04-12 | NULL  |
| Puffball | Diane  | hamster | f    | 1999-03-30 | NULL  |
+----------+--------+---------+------+------------+-------+
4 rows in set (0.00 sec)

2.2 整表刪除,從新加載

編輯文件"1.txt"改正錯誤,而後使用DELETE和LOAD DATA清空並從新裝載表:mysql

MariaDB [tianyuan]> DELETE FROM pet;
MariaDB [tianyuan]> LOAD DATA LOCAL INFILE 'pet.txt' INTO TABLE pet;

效果和上面同樣,然而, 若是這樣操作,必須從新輸入Puffball記錄。正則表達式

2.選擇特殊行

2.1 選擇行

MariaDB [tianyuan]> select * from pet where name='Buffy';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog     | f    | 1994-04-12 | NULL  |
+-------+--------+---------+------+------------+-------+
1 row in set (0.00 sec)

2.2 活用WHERE

WHERE... : 指定條件(=, >, <)

MariaDB [tianyuan]> SELECT * FROM pet WHERE birth > '1994-4-12';
+-------+-------+---------+------+------------+-------+
| name  | owner | species | sex  | birth      | death |
+-------+-------+---------+------+------------+-------+
| yuner | ayuan | person  | f    | 1997-11-13 | NULL  |
+-------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

2.3 WHERE...AND... : 組合條件

MariaDB [tianyuan]> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+
1 row in set (0.00 sec)

2.4 WHERE...OR... : 組合條件

SELECT * FROM pet WHERE species = 'dog' OR species = 'cat';
+--------+--------+---------+------+------------+-------+
| 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  |
+--------+--------+---------+------+------------+-------+
3 rows in set (0.00 sec)

2.5 WHERE...AND...OR... : 組合條件

AND和OR能夠混用,但AND比OR具備更高的優先級。若是你使用兩個操做符,使用圓括號指明如何對條件進行分組是一個好主意。sql

MariaDB [tianyuan]> 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  |
+-------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)

3.選擇特殊列

3.1 寵物的名字與出生日期列組合呈現

MariaDB [tianyuan]> SELECT name, birth FROM pet;
+--------+------------+
| name   | birth      |
+--------+------------+
| Fluffy | 1993-02-04 |
| Claws  | 1994-03-17 |
| Buffy  | 1989-05-13 |
| yuner  | 1997-11-13 |
+--------+------------+
4 rows in set (0.00 sec)

3.2 寵物的主人名單列呈現

MariaDB [tianyuan]> SELECT owner FROM pet;
+--------+
| owner  |
+--------+
| Harold |
| Gwen   |
| Harold |
| ayuan  |
+--------+
4 rows in set (0.00 sec)

3.3 Distinct: 上條名單去重效果

MariaDB [tianyuan]> SELECT DISTINCT owner FROM pet;
+--------+
| owner  |
+--------+
| Harold |
| Gwen   |
| ayuan  |
+--------+
3 rows in set (0.00 sec)

3.4 WHERE子句結合行選擇與列選擇

MariaDB [tianyuan]> 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 |
+--------+---------+------------+
3 rows in set (0.00 sec)

4.分類行

4.1 排序(ORDER BY)

MariaDB [tianyuan]> SELECT name, birth FROM pet ORDER BY birth;
+--------+------------+
| name   | birth      |
+--------+------------+
| Buffy  | 1989-05-13 |
| Fluffy | 1993-02-04 |
| Claws  | 1994-03-17 |
| yuner  | 1997-11-13 |
+--------+------------+
4 rows in set (0.06 sec)

4.2 降序(DESC)

MariaDB [tianyuan]> SELECT name, birth FROM pet ORDER BY birth DESC;
+--------+------------+
| name   | birth      |
+--------+------------+
| yuner  | 1997-11-13 |
| Claws  | 1994-03-17 |
| Fluffy | 1993-02-04 |
| Buffy  | 1989-05-13 |
+--------+------------+
4 rows in set (0.00 sec)

4.3 混合序

species升,birth降

MariaDB [tianyuan]> select name, species, birth FROM pet
    -> ORDER BY species, birth DESC;
+--------+---------+------------+
| name   | species | birth      |
+--------+---------+------------+
| Claws  | cat     | 1994-03-17 |
| Fluffy | cat     | 1993-02-04 |
| Buffy  | dog     | 1989-05-13 |
| yuner  | person  | 1997-11-13 |
+--------+---------+------------+
4 rows in set (0.06 sec)

species降,birth降

MariaDB [tianyuan]> select name, species, birth FROM pet
    -> ORDER BY species DESC, birth DESC;
+--------+---------+------------+
| name   | species | birth      |
+--------+---------+------------+
| yuner  | person  | 1997-11-13 |
| Buffy  | dog     | 1989-05-13 |
| Claws  | cat     | 1994-03-17 |
| Fluffy | cat     | 1993-02-04 |
+--------+---------+------------+
4 rows in set (0.00 sec)

5.日期計算

5.1 寵物年齡計算(簡單的日期計算)

MariaDB [tianyuan]> SELECT name, birth, CURDATE(),
    -> (YEAR(CURDATE())-YEAR(birth))
    -> - (RIGHT(CURDATE(),5)<RIGHT(birth,5))
    -> AS age
    -> FROM pet;
+--------+---------+------------+
| name   | species | birth      |
+--------+---------+------------+
| yuner  | person  | 1997-11-13 |
| Buffy  | dog     | 1989-05-13 |
| Claws  | cat     | 1994-03-17 |
| Fluffy | cat     | 1993-02-04 |
+--------+---------+------------+
4 rows in set (0.00 sec)
  • CURDATE():提早當前精確日期
  • YEAR():提取日期的年部分
  • RIGHT():提取日期的MM-DD (日曆年)部分的最右面5個字符(即月與日)。
    • 比較MM-DD值的表達式部分的值通常爲1或0,若是CURDATE()(今年)的月與日比birth(出生時)的月與日早,則年份應減去1(由於還沒到達生日日期)。

5.2 已死亡寵物年齡計算(帶條件的日期計算)

MariaDB [tianyuan]> select name, birth, death,
    -> (YEAR(death)-YEAR(birth)) - (Right(death,5)<RIGHT(birth,5))
    -> AS age
    -> FROM pet WHERE death IS NOT NULL ORDER BY age;
+--------+------------+------------+------+
| name   | birth      | death      | age  |
+--------+------------+------------+------+
| Fluffy | 1993-02-04 | 1995-02-04 |    2 |
+--------+------------+------------+------+
1 row in set (0.03 sec)

5.3 哪一個寵物下個月過生日

MariaDB [tianyuan]> SELECT name, birth FROM pet WHERE MONTH(birth) = 11;
+-------+------------+
| name  | birth      |
+-------+------------+
| yuner | 1997-11-13 |
+-------+------------+
1 row in set (0.00 sec)

5.3.1 疑問:11能不能換成本月+1?或者取餘

MariaDB [tianyuan]> SELECT name, birth FROM pet WHERE MONTH(birth) = (MONTH(birth)+1);
Empty set (0.00 sec)
MariaDB [tianyuan]> SELECT name, birth FROM pet WHERE MONTH(birth) = (MONTH(birth)+MONTH(birth)/MONTH(birth));
Empty set (0.07 sec)

5.3.2 正解1

MariaDB [tianyuan]> SELECT name, birth FROM pet
    -> WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));

DATE_ADD( )容許在一個給定的日期上加上時間間隔this

5.3.3 正解2

MariaDB [tianyuan]> SELECT name, birth FROM pet 
    -> WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;

獲得的結果是一致的,由於目的同樣。google

+-------+------------+
| name  | birth      |
+-------+------------+
| yuner | 1997-11-13 |
+-------+------------+
1 row in set (0.11 sec)

6.NULL值操做

6.1 算術比較操做符無效

MariaDB [tianyuan]> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
+----------+-----------+----------+----------+
| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |
+----------+-----------+----------+----------+
|     NULL |      NULL |     NULL |     NULL |
+----------+-----------+----------+----------+
1 row in set (0.00 sec)

6.2 IS NULL, IS NOT NULL

MariaDB [tianyuan]> SELECT 1 IS NULL, 1 IS NOT NULL, null IS NULL, null IS NOT NULL;
+-----------+---------------+--------------+------------------+
| 1 IS NULL | 1 IS NOT NULL | null IS NULL | null IS NOT NULL |
+-----------+---------------+--------------+------------------+
|         0 |             1 |            1 |                0 |
+-----------+---------------+--------------+------------------+
1 row in set (0.00 sec)

7.模式匹配

7.1 x%:以x開頭的字符串

MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE 'y%';
+-------+-------+---------+------+------------+-------+
| name  | owner | species | sex  | birth      | death |
+-------+-------+---------+------+------------+-------+
| yuner | ayuan | person  | f    | 1997-11-13 | NULL  |
+-------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

7.2 %x:以x結尾的字符串

MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE '%r';
+-------+-------+---------+------+------------+-------+
| name  | owner | species | sex  | birth      | death |
+-------+-------+---------+------+------------+-------+
| yuner | ayuan | person  | f    | 1997-11-13 | NULL  |
+-------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

7.3 %x%:包含x的字符串

MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE '%un%';
+-------+-------+---------+------+------------+-------+
| name  | owner | species | sex  | birth      | death |
+-------+-------+---------+------+------------+-------+
| yuner | ayuan | person  | f    | 1997-11-13 | NULL  |
+-------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

7.4 x:正好包含x個字符的字符串

MariaDB [tianyuan]> SELECT * FROM pet WHERE name LIKE '______';(6個下劃線)
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | 1995-02-04 |
+--------+--------+---------+------+------------+------------+
1 row in set (0.00 sec)

7.5 MySQL正則表達式

操做中遇到了問題,暫時跳過,語法較爲簡單,只是須要練習。code

MariaDB [tianyuan]> SELECT * FROM pet WHERE name REGEXP '^.{5}$';
ERROR 1139 (42000): Got error 'this version of PCRE is compiled 
without UTF support at offset 0' from regexp

8.計數行

8.1 在pet表中有多少行?(動物數目)

MariaDB [tianyuan]> SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)

8.2 每一個主人有多少寵物(GROUP BY)

MariaDB [tianyuan]> SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+
| owner  | COUNT(*) |
+--------+----------+
| ayuan  |        1 |
| Gwen   |        1 |
| Harold |        2 |
+--------+----------+
3 rows in set (0.00 sec)

8.3 每種動物的數量?

MariaDB [tianyuan]> SELECT species, COUNT(*) FROM pet GROUP BY owner;
+---------+----------+
| species | COUNT(*) |
+---------+----------+
| person  |        1 |
| cat     |        1 |
| cat     |        2 |
+---------+----------+
3 rows in set (0.00 sec)

8.4 按種類和性別組合的動物數量

MariaDB [tianyuan]> SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;
+---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
| person  | f    |        1 |
+---------+------+----------+
4 rows in set (0.00 sec)

8.5 只對狗和貓進行檢索時

MariaDB [tianyuan]> SELECT species, sex, COUNT(*) FROM pet
    -> WHERE species = 'dog' OR species = 'cat'
    -> GROUP BY species, sex;
+---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
+---------+------+----------+
3 rows in set (0.00 sec)

8.6 已知性別的按性別的動物數目

MariaDB [tianyuan]> SELECT species, sex, COUNT(*) FROM pet                  -> WHERE sex IS NOT NULL
    -> GROUP BY species, sex;
+---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
| person  | f    |        1 |
+---------+------+----------+
4 rows in set (0.00 sec)

9.使用1個以上的表

9.1 建立第二張表

MariaDB [tianyuan]> CREATE TABLE event (name VARCHAR(20), date DATE,
    -> type VARCHAR(15), remark VARCHAR(255));
Query OK, 0 rows affected (0.10 sec)

9.2 裝載event.txt文件

LOAD DATA LOCAL INFILE 'desktop/event.txt' INTO TABLE event;
MariaDB [tianyuan]> select * from event;                                          
+--------+------------+--------+-----------------------------+
| name   | date       | type   | remark                      |
+--------+------------+--------+-----------------------------+
| Fluffy | 1995-05-15 | litter | 4 kittens, 3 female, 1 male |
| Buffy  | 1993-06-23 | litter | 5 puppies, 2 female, 3 male |
| Buffy  | 1994-06-19 | litter | 3 puppies, 3 female         |
| Chirpy | 1999-03-21 | vet    | needed beak straightened    |
| Slim   | 1997-08-03 | vet    | broken rib                  |
| Bowser | 1991-10-10 | kennel | NULL                        |
+--------+------------+--------+-----------------------------+
7 rows in set (0.00 sec)

9.3 根據須要來使用兩張表的數據(組成新表呈現)

MariaDB [tianyuan]> SELECT pet.name,
    -> (YEAR(date)-YEAR(birth)) - (RIGHT(date,5)<RIGHT(birth,5)) AS age, 
    -> remark
    -> FROM pet, event
    -> WHERE pet.name = event.name AND event.type = 'litter';
+--------+------+-----------------------------+
| name   | age  | remark                      |
+--------+------+-----------------------------+
| Fluffy |    2 | 4 kittens, 3 female, 1 male |
| Buffy  |  -24 | 5 puppies, 2 female, 3 male |
| Buffy  |  -23 | 3 puppies, 3 female         |
+--------+------+-----------------------------+
3 rows in set (0.00 sec)
相關文章
相關標籤/搜索