天天看點

【MYSQL資料庫開發之三】MYSQL 獲得資料庫和表的資訊、日期計算、對表的删除修改等操作!

通過上一篇的介紹,大家可以建立自己的資料庫和表以及插入表中資料等等,本章繼續介紹更多的資料庫的相關操作;

  1.  檢視所有表單資料:(這裡我直接使用上一篇建立的himidb資料庫與其中的people表進行講解,還不太熟悉的請移步到上一篇的博文)   步驟:(擷取)顯示所有已存在的資料庫->使用himidb資料庫->(擷取)顯示所有表->(擷取)顯示所有表内的資料

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

mysql> show databases;

+--------------------+

| database           |

| information_schema |

| himidb             |

| mysql              |

| performance_schema |

| test               |

5 rows in set (0.00 sec)

mysql> use himidb;

database changed

mysql> show tables;

+------------------+

| tables_in_himidb |

| people           |

1 row in set (0.00 sec)

mysql> select *from people;

+------------+------+----------+------+-----------+------------+

| name       | mz   | city     | sex  | birthday  | deathday   |

| himi       | h    | beijing  | m    | 1989-9-23 | null       |

| himi3      | h    | beijing  | m    | 1989-9-23 | null       |

| inserthimi | h    | anhui    | m    | 1989-9-23 | null       |

| tommy      | m    | chaoxian | w    | 19890823  | 2100-10-10 |

 2. 假設我們修改people中的tommy 的生日為  1990-1-1日:

2.1:第一種方式可以通過txt進行,假設我們已經有一個txt存儲了所有表内資料,并且tommy的生日在txt中已經是最新的了,那麼我們就可以直接如下來進行更改(注意這種方式是删除以前所有表元素直接進行重新添加的操作!如果你沒有之前這些資料就不要使用此方式)

mysql> delete from people;

mysql> load data local infile '/xxx/xxx/people.txt' into table people;

2.2:使用update進行指向性修改;

語句形式: update xx(表名) set xx(item名)   =’xxx新value’ where xx(item索引) =”where中item索引的對應value”;

示例:

mysql> update people set birthday = '1990-1-1' where name = 'tommy';

query ok, 1 row affected (0.11 sec)

rows matched: 1  changed: 1  warnings: 0

| tommy      | m    | chaoxian | w    | 1990-1-1  | 2100-10-10 |

3.檢視特定條件所有資料:

mysql> select *from people where name='himi';

+------+------+---------+------+-----------+----------+

| name | mz   | city    | sex  | birthday  | deathday |

| himi | h    | beijing | m    | 1989-9-23 | null     |

2 rows in set (0.08 sec)

通過以上可以看出是在 select *from xx 後加入指向,比較容易了解;

 繼續檢視哪些是大于1990年之前的額people:

mysql> select *from people where birthday <'1990-1-1';

+------------+------+---------+------+-----------+----------+

| name       | mz   | city    | sex  | birthday  | deathday |

| himi       | h    | beijing | m    | 1989-9-23 | null     |

| himi3      | h    | beijing | m    | 1989-9-23 | null     |

| inserthimi | h    | anhui   | m    | 1989-9-23 | null     |

4 rows in set (0.01 sec)

繼續檢視表中有幾個woman:

mysql> select *from people where sex ="w";

+-------+------+----------+------+----------+------------+

| name  | mz   | city     | sex  | birthday | deathday   |

| tommy | m    | chaoxian | w    | 1990-1-1 | 2100-10-10 |

繼續檢視表中既是woman又是在1990年以前的:(and)

mysql> select *from people where sex ="m" and birthday < '1990-1-1';

4 rows in set (0.00 sec)

繼續檢視表中叫tommy 或者 是beijing地區的:(or)

mysql> select *from people where city='beijing' or birthday <'1990-1-1';

and和or可以混用,但and比or具有更高的優先級。如果使用兩個操作符,使用圓括号進行分組~如下:

mysql> select *from people where (city='beijing' and sex='w') or (city ='chaoxian' and sex = 'w');

  4.檢視所有資料的特定條件:

假如我們需要檢視所有人的性别 和名字:   

mysql> select name,birthday from people;

+------------+-----------+

| name       | birthday  |

| himi       | 1989-9-23 |

| himi3      | 1989-9-23 |

| inserthimi | 1989-9-23 |

| tommy      | 1990-1-1  |

從上面的名字來看,發現了重複的名字,那麼如果隻想讓同一名字隻顯示一個,可以使用 distinct 關鍵字;如下:

mysql> select distinct name from people;

+------------+

| name       |

| himi       |

| himi3      |

| inserthimi |

| tommy      |

來個複雜點的:使用一個where子句結合行選擇與列選擇

mysql> select name from people where city='chaoxian' and birthday = '1990-1-1';+-------+

| name  |

+-------+

| tommy |

選擇出的列根據生日進行排序顯示(提前himi這裡又往表中添加了很多人)

mysql> select birthday from people order by birthday;

+-----------+

| birthday  |

| 1989-9-23 |

| 1990-1-1  |

| 1991-2-3  |

| 1992-5-3  |

7 rows in set (0.00 sec)

選擇出的列根據名字降序進行排序顯示:

mysql> select name from people order by name desc;

| xiao       |

| chinle     |

 5. 日起計算;

mysql提供了幾個函數,可以用來計算日期,例如,計算年齡或提取日期部分:

year()提取日期的年部分,

right(x,y)提取 x 日期的mm-dd (月曆年)  部分的最右面y個字元。

mysql> select name ,birthday,curdate(),(year(curdate())-year(birthday))- (right(curdate(),5)<right(birthday,5)) from people;

+------------+-----------+------------+--------------------------------------------------------------------------+

| name       | birthday  | curdate()  | (year(curdate())-year(birthday))- (right(curdate(),5)<right(birthday,5)) |

| himi       | 1989-9-23 | 2012-04-17 |                                                                       23 |

| himi3      | 1989-9-23 | 2012-04-17 |                                                                       23 |

| inserthimi | 1989-9-23 | 2012-04-17 |                                                                       23 |

| tommy      | 1990-1-1  | 2012-04-17 |                                                                       22 |

| xiao       | 1991-2-3  | 2012-04-17 |                                                                       20 |

| chinle     | 1992-5-3  | 2012-04-17 |                                                                       19 |

 下面我們算下死亡時間和目前時間的內插補點,其中我們排除deathday為null的人。

mysql> select name ,deathday,curdate(),(year(curdate())-year(deathday))- (right(curdate(),5)<right(deathday,5)) from people where deathday is not null;

+--------+------------+------------+--------------------------------------------------------------------------+

| name   | deathday   | curdate()  | (year(curdate())-year(deathday))- (right(curdate(),5)<right(deathday,5)) |

| tommy  | 2100-10-10 | 2012-04-17 |                                                                      -89 |

| xiao   | 1994-1-1   | 2012-04-17 |                                                                       17 |

| chinle | 1994-1-1   | 2012-04-17 |                                                                       17 |

3 rows in set (0.00 sec)

mysql提供幾個日期部分的提取函數,例如year( )、month( )和dayofmonth( )。在這裡month()是适合的函數。為了看它怎樣工作,運作一個簡單的查詢: 

mysql> select name ,birthday, year(birthday),month(birthday) ,dayofmonth(birthday) from people;

+------------+-----------+----------------+-----------------+----------------------+

| name       | birthday  | year(birthday) | month(birthday) | dayofmonth(birthday) |

| himi       | 1989-9-23 |           1989 |               9 |                   23 |

| himi3      | 1989-9-23 |           1989 |               9 |                   23 |

| inserthimi | 1989-9-23 |           1989 |               9 |                   23 |

| tommy      | 1990-1-1  |           1990 |               1 |                    1 |

| xiao       | 1991-2-3  |           1991 |               2 |                    3 |

| chinle     | 1992-5-3  |           1992 |               5 |                    3 |

練習:找到一個下個月是幾月份:

mysql> select name,birthday,month(birthday)+1 from people;

+------------+-----------+-------------------+

| name       | birthday  | month(birthday)+1 |

| himi       | 1989-9-23 |                10 |

| himi3      | 1989-9-23 |                10 |

| inserthimi | 1989-9-23 |                10 |

| tommy      | 1990-1-1  |                 2 |

| xiao       | 1991-2-3  |                 3 |

| chinle     | 1992-5-3  |                 6 |