MySQL增删改查基础语句
1.增,删,改,查 操作
增:
除自动增长列,有默认值和允许为空的列可以不输入数值,其它列必须要有值。
insert into student(列列表) values(值列表) 当所有列都有数据时,则可以省略列列表
insert into student(name,sex,age,address,phone,email,gradeid)
values('肖月','女',20,'广州','13390956678','[email protected]',2);
insert into student(name,sex,age,address,phone,email,gradeid)
values('赵华','男',20,'广州','13390678123','[email protected]',2);
insert into student
values(null,'周全','男',21,'广州天河','13566909780','[email protected]',3)
删
语法 DELETE FROM student [条件] 删除的过程中要注意主表及子表的情况,只有先删除子表的数据,才能删除主表的数据。
delete from student where id=5
修改
语法 update student set name=’’,sex=’’,age=25 [where 条件]
update student set name=‘张小明’,sex=‘男’,age=25 where id=1
查询 select
分为: 普通,分组查询,模糊查询,连接查询
select * from student -- 查询所有行所有列
select name,sex,age from student -- 查询所有行部分列
select name,sex,age from student where sex='男' -- 查询部分行部分列
模糊查询 like in between is NULL
like 象… 通配符 _表示一个任意字符 %表示0-N个任意字符
select * from student where name like '%张%'
in 包含在…里面 查询的列值要与条件完全匹配
select * from student where address in('韶关','广州')
between 是一个范围,要求查询的列值在这个区间,包含上限及下限这两个值,小的值必须在前,大的值必须在后面
select * from student where age between 20 and 22
– 同等于
select * from student where age>=22 and age <=20
is null 查询某列值为NULL的数据行
select * from student where email = null
select * from student where email = ''
select * from student where email is null
分组查询,需要依赖统计函数来处理。
聚合函数,统计函数 sum,max,min,avg,count
字符串函数,对字符串进行处理
数学函数,对数值进行处理
日期函数,对日期时间进行处理
系统函数,获取到系统运行时的一些数据
select sum(age),max(age),min(age),avg(age) from student
select count(email) from student
分组查询关键点:根据什么分类,再运用什么聚合函数
统计男,女学生各有多少人? count sex
select sex,count(*) from student group by sex
统计男女学生分别的平均年龄? avg sex
select sex,avg(age) from student group by sex
– 统计每个年级有多少人? count gradeId
select gradeId,count(*) from student group by gradeId
分组查询的进一步筛选 having
统计人数超过2人(包含)的年级信息
select gradeId,count(*) from student group by gradeId having count(*)>=2