在使用mysql数据库时,我们最常做的是4件事,增删改查,也就是俗称的CRUD(create read update delete),接下我们来研究研究CRUD。
插入数据
基本语法
insert into 表名 [(字段列表)] values (值列表);
insert into student values(1,"张三",'男');
select * from student;
+----+-------+-----+
| id | names | sex |
+----+-------+-----+
| 1 | 张三 | 男 |
+----+-------+-----+
主键冲突时
insert into 表名[(字段列表:包含主键)] values(值列表) on duplicate key update 字段 = 新值;
示例:
insert into student values(1,"张三",'女') on duplicate key update id=2;
select * from student;
+----+-------+-----+
| id | names | sex |
+----+-------+-----+
| 2 | 张三 | 男 |
+----+-------+-----+
从表中已有的数据赋值数据
insert into 表名[(字段列表)] select 字段列表/* from 数据表名;
insert student(names,sex) select names,sex from student;
select * from student;
+----+-------+-----+
| id | names | sex |
+----+-------+-----+
| 2 | 张三 | 男 |
| 3 | 张三 | 男 |
+----+-------+-----+
修改数据
UPDATE 表名 SET 字段名=字段值 WHERE 条件
update student set names='李四',sex='女' where id=3;
select * from student;
+----+-------+-----+
| id | names | sex |
+----+-------+-----+
| 2 | 张三 | 男 |
| 3 | 李四 | 女 |
+----+-------+-----+
删除数据
delete from 表名 [where ] [limit ];
delete * from student limit 1;
select * from student;
+----+-------+-----+
| id | names | sex |
+----+-------+-----+
| 3 | 李四 | 女 |
+----+-------+-----+
查询数据
- 查询所有数据
select * from student;
+----+-------+-----+
| id | names | sex |
+----+-------+-----+
| 3 | 李四 | 女 |
+----+-------+-----+
- 查询特定字段及待条件的数据
select [字段,字段] from 表名 WHERE 条件 LIMI M,N
select names from student where id>=5;
+--------+
| names |
+--------+
| 姬雪 |
| 武则天 |
| 李世民 |
+--------+
select names,sex from student where id>=5 limit 2;
+--------+-----+
| names | sex |
+--------+-----+
| 姬雪 | 女 |
| 武则天 | 女 |
+--------+-----+
- 查询不重复的数据
select distinct [字段,字段] from 表名
distinct depart from teacher;
+------------+
| depart |
+------------+
| 计算机系 |
| 电子工程系 |
+------------+
- 查询between and条件
mysql的between and包含左右边界
select * from Score where Degree between 60 and 80;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
| 107 | 6-166 | 79 |
| 108 | 3-105 | 78 |
| 109 | 3-105 | 76 |
| 109 | 3-245 | 68 |
+-----+-------+--------+
- 查询in条件
select * from Score where Degree in(85,86,88);
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 101 | 6-166 | 85 |
| 103 | 3-245 | 86 |
| 105 | 3-105 | 88 |
+-----+-------+--------+
- 查询排序order by
desc降序,asc升序(默认)
select * from student order by class desc
+-----+-------+------+---------------------+-------+
| sno | sname | ssex | sbirthday | class |
+-----+-------+------+---------------------+-------+
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
+-----+-------+------+---------------------+-------+
- 表内连接查询(忽略不相等的数据)
查询所有学生的Sname、Cname和Degree列。
select Sname,Cname,Degree from student,course,score where student.Sno=score.Sno and course.Cno=score.Cno
join .. on 写法:
select Sname,Cname,Degree from student join score on student.Sno=score.Sno join course on course.Cno=score.Cno
+-------+--------------+--------+
| Sname | Cname | Degree |
+-------+--------------+--------+
| 李军 | 鏁板瓧鐢佃矾 | 85 |
| 陆君 | 计算机导论 | 92 |
| 陆君 | 操作系统 | 86 |
| 匡明 | 计算机导论 | 88 |
| 匡明 | 操作系统 | 75 |
| 匡明 | 鏁板瓧鐢佃矾 | 79 |
| 王丽 | 计算机导论 | 91 |
| 王丽 | 鏁板瓧鐢佃矾 | 79 |
| 曾华 | 计算机导论 | 78 |
| 曾华 | 鏁板瓧鐢佃矾 | 81 |
| 王芳 | 计算机导论 | 76 |
| 王芳 | 操作系统 | 68 |
+-------+--------------+--------+
- 表左连接查询(保存左侧不相等的数据)
select Sname,degree from student left join score on student.sno=score.sno;
+-------+--------+
| Sname | degree |
+-------+--------+
| 李军 | 85 |
| 陆君 | 92 |
| 陆君 | 86 |
| 匡明 | 88 |
| 匡明 | 75 |
| 匡明 | 79 |
| 王丽 | 91 |
| 王丽 | 79 |
| 曾华 | 78 |
| 曾华 | 81 |
| 王芳 | 76 |
| 王芳 | 68 |
| 警察 | NULL |
+-------+--------+
- 表右连接询(保存右表不相等的数据)
相比与左连接查询,只是表互换了位置而已
select Sname,degree from score right join student on student.sno=score.sno;
+-------+--------+
| Sname | degree |
+-------+--------+
| 李军 | 85 |
| 陆君 | 92 |
| 陆君 | 86 |
| 匡明 | 88 |
| 匡明 | 75 |
| 匡明 | 79 |
| 王丽 | 91 |
| 王丽 | 79 |
| 曾华 | 78 |
| 曾华 | 81 |
| 王芳 | 76 |
| 王芳 | 68 |
| 警察 | NULL |
+-------+--------+
闲言碎语
mysql的查询语句有很多技巧和规则,在此只列了一些基本的,有时间会单独写一篇文章来详细解释。