MySQL3
DML 增删改表中的数据
1 添加数据
insert into 表名(列名1,列名2,....列名n) values(值1,值2,....值n);
insert into stu(id,name,age) values(1,'张三',18);
注意: 列名和值要一一对应
如果表名后不定义列名,则默认给所有列添加值
除了数字类型,其他类型要用引号(单双都可以)引起来
2 删除数据
delete from 表名 where 条件
注意 如果不加条件,则删除表中所有记录
如果要删除所有记录 truncate table 表名; 先删除表,然后创建一张一模一样的表
3 修改数据
update 表名 set 列名1=值1,列名2=值2,...... where 条件;
如果不加任何条件,则会将表中所有记录全部修改
DQL 查询表中的记录
select * from 表名; 查询所有的数据
语法
select
字段列表
from
表名列表
where
条件列表
group by
分组字段
having
分组之后的条件
order by
排序
limit
分页
基础查询
多个字段的查询
select 字段名1,字段名2,....from 表名;
select address from student;
注意 如果查询所有的字段,可以用*代替字段列表
去除重复
distinct
select distinct address from student;
select distinct name,address from student;
只有两个内容完全一样才可以去除
计算列
一般可以使用四则运算计算一些列的值(一般只会进行数值型计算)
select name,math,english, math+english from student;
ifnull(表达式1,表达式2) null参与的运算,计算结果都为null
表达式1代表哪个字段需要判断是否为null
表达式2代表替换的值
select name,math,english,math+ifnull(english,0) from student;
起别名
as
select name,math as 数学,english as 英语,math+ifnull(english,0) as 总分 from student;
条件查询
where子句后跟条件
运算符
>、<、<=、>=、=、,<>
select * from student where age>20;
select * from student where age=20;
select * from student where age!=20; 或
select * from student where age<>20;
between...and
select * from student where age between 20 and 30;
in
select * from student where age in(22,18,25)
like 模糊查询
占位符
_: 单个任意字符
%:多个任意字符
select * from student where name like '马%';
select * from student where name like '_化%';
select * from student where name like '___';
select * from student where name like '%马%'; //包含马这个字
is null
select * from student where english is null;
select * from student where english is not null;
and或&&
select * from student where age >=20 and age<=30;
or或||
select * from student where age=22 or age=19 or age=25;
not或!
select * from student where age!=20;

浙公网安备 33010602011771号