通过上一篇的介绍,大家可以创建自己的数据库和表以及插入表中数据等等,本章继续介绍更多的数据库的相关操作;
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 |
- | 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 |
- +------------+------+----------+------+-----------+------------+
- 5 rows in set (0.00 sec)
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
- mysql> select *from people;
- +------------+------+----------+------+-----------+------------+
- | name | mz | city | sex | birthday | deathday |
- +------------+------+----------+------+-----------+------------+
- | himi | h | beijing | m | 1989-9-23 | NULL |
- | 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 | 1990-1-1 | 2100-10-10 |
- +------------+------+----------+------+-----------+------------+
- 5 rows in set (0.00 sec)
- mysql> select *from people where name='himi';
- +------+------+---------+------+-----------+----------+
- | name | mz | city | sex | birthday | deathday |
- +------+------+---------+------+-----------+----------+
- | himi | h | beijing | m | 1989-9-23 | NULL |
- | 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 |
- | 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)
- mysql> select *from people where sex ="w";
- +-------+------+----------+------+----------+------------+
- | name | mz | city | sex | birthday | deathday |
- +-------+------+----------+------+----------+------------+
- | tommy | m | chaoxian | w | 1990-1-1 | 2100-10-10 |
- +-------+------+----------+------+----------+------------+
- 1 row in set (0.00 sec)
- mysql> select *from people where sex ="m" and birthday < '1990-1-1';
- +------------+------+---------+------+-----------+----------+
- | name | mz | city | sex | birthday | deathday |
- +------------+------+---------+------+-----------+----------+
- | himi | h | beijing | m | 1989-9-23 | NULL |
- | 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.00 sec)
- mysql> select *from people where city='beijing' or birthday <'1990-1-1';
- +------------+------+---------+------+-----------+----------+
- | name | mz | city | sex | birthday | deathday |
- +------------+------+---------+------+-----------+----------+
- | himi | h | beijing | m | 1989-9-23 | NULL |
- | 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.00 sec)
- mysql> select *from people where (city='beijing' and sex='w') or (city ='chaoxian' and sex = 'w');
- +-------+------+----------+------+----------+------------+
- | name | mz | city | sex | birthday | deathday |
- +-------+------+----------+------+----------+------------+
- | tommy | m | chaoxian | w | 1990-1-1 | 2100-10-10 |
- +-------+------+----------+------+----------+------------+
- 1 row in set (0.00 sec)
4.查看所有数据的特定条件:
假如我们需要查看所有人的性别 和名字:
- mysql> select name,birthday from people;
- +------------+-----------+
- | name | birthday |
- +------------+-----------+
- | himi | 1989-9-23 |
- | himi | 1989-9-23 |
- | himi3 | 1989-9-23 |
- | insertHimi | 1989-9-23 |
- | tommy | 1990-1-1 |
- +------------+-----------+
- 5 rows in set (0.00 sec)
- mysql> select distinct name from people;
- +------------+
- | name |
- +------------+
- | himi |
- | himi3 |
- | insertHimi |
- | tommy |
- +------------+
- 4 rows in set (0.00 sec)
- mysql> select name from people where city='chaoxian' and birthday = '1990-1-1';+-------+
- | name |
- +-------+
- | tommy |
- +-------+
- 1 row in set (0.00 sec)
- mysql> select birthday from people order by birthday;
- +-----------+
- | birthday |
- +-----------+
- | 1989-9-23 |
- | 1989-9-23 |
- | 1989-9-23 |
- | 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;
- +------------+
- | name |
- +------------+
- | xiao |
- | tommy |
- | insertHimi |
- | himi3 |
- | himi |
- | himi |
- | chinle |
- +------------+
- 7 rows in set (0.00 sec)
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 |
- | 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 |
- +------------+-----------+------------+--------------------------------------------------------------------------+
- 7 rows in set (0.00 sec)
- 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> 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 |
- | 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 |
- +------------+-----------+----------------+-----------------+----------------------+
- 7 rows in set (0.00 sec)
- mysql> select name,birthday,month(birthday)+1 from people;
- +------------+-----------+-------------------+
- | name | birthday | month(birthday)+1 |
- +------------+-----------+-------------------+
- | himi | 1989-9-23 | 10 |
- | 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 |
- +------------+-----------+-------------------+
- 7 rows in set (0.00 sec)
本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/908923,如需转载请自行联系原作者