Mysql的DML基本操作

1.为数据库的表插入数据    两种方法:

insert into teacher values('001','张三','中国')

insert into 表名 (属性名1,属性名2) values ('001','李四')

2.同时插入多条记录 

insert into teacher [属性名] values ('数值1','数值2')

3.把查询出来的数据,直接插入到另外表中

insert into 新表名 select * from  旧表名

4.更新数据  update 表名 set 属性名1=取值1,属性名2=取值2 where 筛选条件

例子:update classer set name=‘张三’ where id=1;

5.删除数据   delete from 表名  删除表中所有数据

delete from 表名 where id=1  删除一条数据

6.查询语句

select  属性列表 from 表名 where 条件表达式  group by 属性名1[having 条件表达式] order by 属性名2 dese

7.多表查询

left join 左连接。两个表连接,如果左边表中的数据在右边表中没有,那么就显示左边表的数据。

right join 右连接  与左联接相反,显示右边的数据

inner join 内连接  只会显示两个表都有的数据。

8.带in的条件查询

select * from teacher where id in(1.2,3)

9.带or的条件查询

条件表达式1 or 条件表达式2 or 条件表达式3

例子:select * from teacher where id=1 or id=2

10.带and的条件查询

条件表达式1 and 条件表达式2

例子:select * from teacher where id=1 and name=‘张三’

11.查询结果去除重复   select distinct 要去重的字段 from teacher;

例子:select  distinct  phone from teacher;

12.限制查询数量

例子:select * from teacher limit 5;  只查询5条数据

select * from teacher limit 10,20;查询从第10条道20条的数据;

13.between...and查询

select * from teacher where score between 60 and 90;

14.对查询结果进行排序

select * from teacher where sex=‘男’ order by score;

15.使用聚合函数进行查询

聚合函数包含sum(),count(),avg(),max(),min(),group by 通常和聚合函数连起来使用。

例子:select count(*) from student;  查询表中数据数量

16.sum()是对某个字段求和/avg()是对某个字段求平均值/max()是对某个字段求最大值/min()是对某个字段求最小值

例子:select sum(score) from student;

17.group by 可以将查询结果按某个字段进行分组,该字段中值相等的为一组。

例子:group by 字段名 [having 条件表达式]

select * from student group by sex; 学生表中以男女分出来

18.group by 和聚合函数连接使用的例子:

select count(*),sex from student group by sex;  统计出学生表中男女人数

19.group by 后多个字段分组,比如统计每个班级的男女人数,例子:select ...... group by student.sex,class.class_name

20.union 合并结果集

使用union可以把多个查询结果合并为一个结果,但是要求被合并的字段个数必须相同,而且有相同的数据类型。

例子:select 字段列表1 from teacher1  uinon select 字段列表2 from teachere2;

21.子查询。当一个select语句能返回单个数据或者一列的数值,且该select语句嵌套在另一个sql语句中,那么该select语句就jiao'zuo子查询,一般都用小括号区分,包含这个select语句的sql语句叫做主查询。

posted on 2018-12-19 11:18  大潘NO_1  阅读(333)  评论(0编辑  收藏  举报