目录结构
本章目录
一、插入insert:
方法一:insert标准插入数据写法
方法二:set插入数据写法
方法三:请看本章最后一个案例
二、插入update:
方法一:单表更新记录
方法二:多表更新记录
三、删除记录detele
第一种方法:单表删除
第二种:多表删除暂未学习
四、查询记录select
五、select表达式、group by 分组、having语句设置分组条件,order by查询结果排序,limit语句限制查询数量
一、group by 分组
二、having语句设置分组条件
三、order by语句对查询结果排序
四、limit语句限制查询数量
五、最后:你查找到的数据写入到一张新的数据表(insert方法三案例)
案例前准备工作
- 案例准备:首先创建一张数据表,如下:
mysql> create table users(
-> id smallint unsigned PRIMARY key auto_increment,
-> username varchar(20) not null,
-> password varchar(32) not null,
-> age tinyint unsigned not null default 10,
-> sex boolean);
Query OK, 0 rows affected (0.12 sec)
一、插入insert:
- 插入写法有三种
1、INSERT [INTO] tab_name[(col_name,…)] {values|value}({expr|default},…),(…),…
2、insert tbl_name set col_name = {exp|default},…
3、insert two (username) select username from users where age>=30;//演示在本章最后一个案例
方法一:insert标准插入数据写法
//前面也有简单的介绍:http://blog.csdn.net/bobo89455100/article/details/72626337
案例第二步:插入数据,使用多条插入。
//插入四条数据
mysql> insert users values
(default,'tom',123,25,1),
(default,'jek',123,8,2),
(default,'jek',1234,7+8,default),
(null,'laowang',md5(1234),13,default);
Query OK, 2 rows affected (0.10 sec)//插入成功
mysql> select * from users;//查询插入结果:
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | tom | 123 | 25 | 1 |
| 2 | jek | 123 | 8 | 2 |
| 3 | jek | 1234 | 15 | NULL |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 13 | NULL |
+----+----------+----------------------------------+-----+------+
方法二:set插入数据写法
- 注意:
1、此方法可以使用子查询(subquery)
2、只能一次性插入一条数据
- 语法
语法:insert tbl_name set col_name = {exp|default},…
讲解:插入数据表 set 字段=值,字段=值,…
- 案例:
mysql> insert users set username='xiaosan',password='233',age='18',sex='2';
Query OK, //插入成功
mysql> select * from users;
//插入前
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | tom | 123 | 25 | 1 |
| 2 | jek | 123 | 8 | 2 |
| 3 | jek | 1234 | 15 | NULL |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 13 | NULL |
+----+----------+----------------------------------+-----+------+
//插入后,最后一条数据成功
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | tom | 123 | 25 | 1 |
| 2 | jek | 123 | 8 | 2 |
| 3 | jek | 1234 | 15 | NULL |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 13 | NULL |
| 5 | xiaosan | 233 | 18 | 2 |
+----+----------+----------------------------------+-----+------+
5 rows in set (0.00 sec)
方法三:请看本章最后一个案例
二、插入update:
方法一:单表更新记录
语句:
update table_reference set col_name1={expr1|default},…[where where_condition]
注意:更新数据如果省略where 条件,将导致表中的数据全部更新
案例一:让表中所有的年龄age都增加5岁
mysql> update users set age = age+5;
Query OK, 5 rows affected (0.08 sec)//有五条数据更新成功
mysql> select * from users;
//更新前
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | tom | 123 | 25 | 1 |
| 2 | jek | 123 | 8 | 2 |
| 3 | jek | 1234 | 15 | NULL |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 13 | NULL |
| 5 | xiaosan | 233 | 18 | 2 |
+----+----------+----------------------------------+-----+------+
//更新后,所有的age列都增加了5岁,如下
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | tom | 123 | 30 | 1 |
| 2 | jek | 123 | 13 | 2 |
| 3 | jek | 1234 | 20 | NULL |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 18 | NULL |
| 5 | xiaosan | 233 | 23 | 2 |
+----+----------+----------------------------------+-----+------+
- 案例二:讲年龄的数据增加2,同时sex数据全部变成0
mysql> update users set age = age+2,sex=0;
Query OK, 5 rows affected (0.23 sec)//五条数据被更新
mysql> select * from users;
//更新前
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | tom | 123 | 30 | 1 |
| 2 | jek | 123 | 13 | 2 |
| 3 | jek | 1234 | 20 | NULL |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 18 | NULL |
| 5 | xiaosan | 233 | 23 | 2 |
+----+----------+----------------------------------+-----+------+
//更新后,年龄的数据全部增加了2岁,而sex的值全变成了0.
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | tom | 123 | 32 | 0 |
| 2 | jek | 123 | 15 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 20 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
+----+----------+----------------------------------+-----+------+
- 案例三:添加条件来更新数据,让所有偶数的年龄加上10岁。
mysql> update users set age = age+10 where id%2=0;
Query OK,
Rows matched: 2 Changed: 2 Warnings: 0//两条数据被更新
mysql> select * from users;//查看对比
//更新前,偶数2、4的数值为15,20
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | tom | 123 | 32 | 0 |
| 2 | jek | 123 | 15 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 20 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
+----+----------+----------------------------------+-----+------+
更新后,偶数2、4的数值为25,30
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | tom | 123 | 32 | 0 |
| 2 | jek | 123 | 25 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
+----+----------+----------------------------------+-----+------+
5 rows in set (0.00 sec)
方法二:多表更新暂未学习(初学mysql)
三、删除记录detele
第一种方法:单表删除
语句
语句:delete from tbl_name [where where_condition]
讲解:从tbl_name数据表 删除 where条件
注意:
1、如果省略where条件,将删除数据表中的所有数据
2、我们删除了某条数据之后,例如删除id为1的数据,再次加入新的数据,数据的id也只会最大id值加1,而不会去补充刚删除的id
案例一:删除第一条数据
mysql> delete from users where id = 1;
Query OK, //删除成功
mysql> select * from users;
//删除前
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | tom | 123 | 32 | 0 |
| 2 | jek | 123 | 25 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
+----+----------+----------------------------------+-----+------+
//删除后,第一条数据不存在了
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 2 | jek | 123 | 25 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
+----+----------+----------------------------------+-----+------+
第二种:多表删除暂未学习
四、查询记录select
注意:
我们在操作数据库的时候,最常用的就是select。也是重点研究的角色select selct_expr,[条件];
查询表达式:
a、每一个表达式表示想要的一列,必须有一个。
b、多个列之间以英文逗号隔开
c、星号()表示所有列。tbl_name.可以表示命令表的所有列。
d、查询表达式可以使用[AS] alias_name为其赋予别名
e、别名可用于group by,ordre by或者havihg自居案例一:查询我们的users所有数据
//(tbl_name.*可以表示命令表的所有列。)
mysql> select * from users;
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 2 | jek | 123 | 25 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
+----+----------+----------------------------------+-----+------+
4 rows in set (0.00 sec)
- 案例二:只查询id,username前两列(注意:查询表达式的顺序可以不按顺序写,同样结果的书序会跟查询的顺序保持一致)
mysql> select id,username from users;
+----+----------+
| id | username |
+----+----------+
| 2 | jek |
| 3 | jek |
| 4 | laowang |
| 5 | xiaosan |
+----+----------+
案例三:我们使用:表明.列明。可查询多表不同列验证 ,select * from users表示所有列。tbl_name.*可以表示命令表的所有列。
tbl_name.*写法的好处 (例如下面代码,users.id)
使用tbl_name.*就可以清晰的分清楚这个字段属于那张表。
mysql> select users.id,users.username from users;
+----+----------+
| id | username |
+----+----------+
| 2 | jek |
| 3 | jek |
| 4 | laowang |
| 5 | xiaosan |
+----+----------+
4 rows in set (0.00 sec)
- 案例四:使用别名来查询表,例如( id as userid)
1、字段的别名也会影响以后的结果集(和字段的顺序会影响结果集一样)
、其中AS,可以用也可以不用,但是建议使用,避免.
mysql> select id as userid,username as uname from users;
+--------+---------+
| userid | uname |
+--------+---------+
| 2 | jek |
| 3 | jek |
| 4 | laowang |
| 5 | xiaosan |
+--------+---------+
4 rows in set (0.00 sec)
五、select表达式、group by 分组、having语句设置分组条件,order by查询结果排序,limit语句限制查询数量
一、select表达式(where)
对于记录进行过滤,如果没有指定where条件,则显示所有记录
在where表达式中,可以使用mysql支持的函数或运算符
一、group by 分组
语法
[group by {col_name|position} [asc|desc],…]
asc:升序
desc:降序案例
mysql> select * from users;
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 2 | jek | 123 | 25 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
+----+----------+----------------------------------+-----+------+
//使用:group by进行分组,sex就会分为两组,因为只有值0和null
mysql> select sex from users group by sex;
+------+
| sex |
+------+
| 0 |
+------+
1 row in set (0.03 sec)
二、having语句设置分组条件
having是分组,group by后的筛选条件,分组后的数据组内再筛选
聚合函数:
“聚合函数”——SQL语言中一种特殊的函数。例如SUM, COUNT, MAX, AVG等。这些函数和其它函数的根
注意
where则是在分组前筛选,本区别就是它们一般作用在多条记录上。
d案例:
mysql> select * from users;//数据表中的数据
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 2 | jek | 123 | 25 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
| 6 | laozhang | 123 | 13 | 5 |
| 7 | laoxia | 345 | 11 | 5 |
| 8 | laopo | 123 | 13 | 8 |
| 9 | laohu | 345 | 11 | 12 |
+----+----------+----------------------------------+-----+------+
8 rows in set (0.00 sec)
//sex值相同的放在一组,所以这一同有4组数据,例如0组就有4条数据。
mysql> select sex from users group by sex;
+------+
| sex |
+------+
| 0 |
| 5 |
| 8 |
| 12 |
+------+
4 rows in set (0.00 sec)
//这里的>=2 代表的意思是,每组的数据量大于2的。0组中有4条数据,5组的有两条数据。所有结果为如
//特别注意:
//1、我们在分组的时候,sex值相同的会放在一组,所以这里只有两个不一样的组,值=0的组和组=null的
//2、我们在使用hvaing count(id)>=2的时候(2代表的是 id(即不同的条数)条数sex出现相同的次数),就是我们根据id来做条件,千万不要吧>=2和sex的具体值来比较。因为它是出现相同的次数。
//3、因为sex值为0的条数大于等于2条(老师数据表中1/2/3/4/5/6/7/8条的sex都是0)。而null只有一条。所以这里的查询后的结果是0分组。(注意,是0的分组,而不是0值)!
下:
mysql> select sex from users group by sex having count(id)>=2;
+------+
| sex |
+------+
| 0 |
| 5 |
+------+
2 rows in set (0.02 sec)
三、order by语句对查询结果排序
语句:
语句:select * from users order by id desc;
讲解:查询users数据表,根据id降序排序
案例
mysql> select * from users;
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 2 | jek | 123 | 25 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
| 6 | laozhang | 123 | 13 | 5 |
| 7 | laoxia | 345 | 11 | 5 |
| 8 | laopo | 123 | 13 | 8 |
| 9 | laohu | 345 | 11 | 12 |
+----+----------+----------------------------------+-----+------+
8 rows in set (0.00 sec)
//降序排序
mysql> select * from users order by id desc;
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 9 | laohu | 345 | 11 | 12 |
| 8 | laopo | 123 | 13 | 8 |
| 7 | laoxia | 345 | 11 | 5 |
| 6 | laozhang | 123 | 13 | 5 |
| 5 | xiaosan | 233 | 25 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 2 | jek | 123 | 25 | 0 |
+----+----------+----------------------------------+-----+------+
8 rows in set (0.00 sec)
//根据age的年龄大小降序排序
mysql> select * from users order by age;
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 7 | laoxia | 345 | 11 | 5 |
| 9 | laohu | 345 | 11 | 12 |
| 6 | laozhang | 123 | 13 | 5 |
| 8 | laopo | 123 | 13 | 8 |
| 3 | jek | 1234 | 22 | 0 |
| 2 | jek | 123 | 25 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
+----+----------+----------------------------------+-----+------+
8 rows in set (0.00 sec)
四、limit语句限制查询数量
默认返回你查找到的所有结果
//我们数据表默认排序如下
mysql> select * from users;
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 2 | jek | 123 | 25 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
| 6 | laozhang | 123 | 13 | 5 |
| 7 | laoxia | 345 | 11 | 5 |
| 8 | laopo | 123 | 13 | 8 |
| 9 | laohu | 345 | 11 | 12 |
+----+----------+----------------------------------+-----+------+
8 rows in set (0.00 sec)
//注意,id的值不代表它在数据表中的索引。所以这里limit就是限制我们查询的数据为前两条。所以返
回结果如下。
mysql> select * from users limit 2;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 2 | jek | 123 | 25 | 0 |
| 3 | jek | 1234 | 22 | 0 |
+----+----------+----------+-----+------+
2 rows in set (0.00 sec)
//limit2,2的意思就是,从0下表小时算起,从2下表开始显示2条数据。
mysql> select * from users limit 2,2;
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
+----+----------+----------------------------------+-----+------+
2 rows in set (0.00 sec)
五、最后:你查找到的数据写入到一张新的数据表
我们学会了where条件和这些函数,我们使用insert第三种方法来演示
//首先创建一个张新的数据表,里面没有任何的数据
mysql> create table two(
-> id smallint unsigned primary key auto_increment,
-> username varchar(20) );
Query OK, 0 rows affected (0.22 sec)
//把users数据库age大于25的数据,查询出来并写入到新的数据表。操作如下:
//第一步,我们要查询的数据,
mysql> select * from users;
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 2 | jek | 123 | 25 | 0 |
| 3 | jek | 1234 | 22 | 0 |
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
| 6 | laozhang | 123 | 13 | 5 |
| 7 | laoxia | 345 | 11 | 5 |
| 8 | laopo | 123 | 13 | 8 |
| 9 | laohu | 345 | 11 | 12 |
+----+----------+----------------------------------+-----+------+
8 rows in set (0.00 sec)
//第二步我们,查询数据表下表从2开始的两条数据
mysql> select * from users limit 2,2;
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 4 | laowang | 81dc9bdb52d04dc20036dbd8313ed055 | 30 | 0 |
| 5 | xiaosan | 233 | 25 | 0 |
+----+----------+----------------------------------+-----+------+
2 rows in set (0.00 sec)
// 我们插入username参数,从users数据表中查找,并且符合age>=30条件的数据。查看我们新插入数据
的two表,结果如下:
mysql> insert two (username) select username from users where age>=30;
Query OK, 1 row affected (0.08 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from two;
+----+----------+
| id | username |
+----+----------+
| 1 | laowang |
+----+----------+
1 row in set (0.00 sec)
mysql>