通過上一篇的介紹,大家可以建立自己的資料庫和表以及插入表中資料等等,本章繼續介紹更多的資料庫的相關操作;
1. 檢視所有表單資料:(這裡我直接使用上一篇建立的himiDB資料庫與其中的people表進行講解,還不太熟悉的請移步到上一篇的博文) 步驟:(擷取)顯示所有已存在的資料庫->使用himidb資料庫->(擷取)顯示所有表->(擷取)顯示所有表内的資料
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 |
本文轉自 xiaominghimi 51CTO部落格,原文連結:http://blog.51cto.com/xiaominghimi/908923,如需轉載請自行聯系原作者