- 修改表中数据
修改整个字段
update 表名 set 字段=新值;
eg:update person set gender="男";
修改某一条数据中的某个字段的值
update 表名 set 字段=新值 where 条件;
eg:update person set gender="男" where id=4|name="美羊羊";
修改多条数据中的某个字段中的值
update 表名 set 字段=新值 where 条件 or 条件;
- 删除表中数据
删除整个字段
delete from 表名;
删除某一条字段
delete from 表名 where 条件;
逻辑删除 本质:给表中增加一个字段 修改数据
eg:给表中增加一个字段 isdelete
指定默认值为0 删除 1
alter table person add isdelete int default 0;
想要实现逻辑删除美羊羊 修改美羊羊中的isdelete的值为1就可以
update person set isdelete=1 where id=4;
- 查询表中数据
查询表中所有数据
select * from 表名;
查询表中指定字段数据
select 字段1,字段2 from 表名;
查询指定数据
select * from 表名 where 条件;
- 起别名
给表名起别名
select 别名,原名 别名,原名 from 表名 as 别名; 仅限于在当前这条sql语句起作用,并不会改变原来的表
给字段起别名
select name as 别名,原名 as 别名 from 表格;
- 去重
select distinct 字段名 from 表名;
eg;select distinct gender from stu;
- 条件查询
比较运算符 > < = >= <= !=
select * from 表名 where 条件;
eg:年龄大于18 select * from stu where age>18;
年龄小于18 select * from stu where age<18;
逻辑运算符 and or not
select * from 表名 where 条件;
eg:18-28之间的信息 select * from stu where 18<age and age<28;
18岁以上或者身高超过180的信息 select * from stu where age>18 or height>180;
不是18岁以内的女性 select * from stu where not (age<18 and gender="女");
范围查询 in not in (between a and b:查找a-b之间数据,包含ab) (not between a and b)
in 通常用于不连续的查询
select * from 表名 where 条件;
eg:年龄是18和34 select * from stu where age in (18,34);
年龄在18-34之间 select * from stu where age between 18 and 34;
年龄不在18-34之间 select * from stu where age not between 18 and 34;
空|非空判断 is | is not
eg:判断身高为空 select * from stu where height is null;
判断身高不为空 select * from stu where height is not null;
模糊查询 like %匹配0个或无数 _匹配单个任意字符
eg:查询姓名中以小开头的名字 select * from stu where name like "小%";
查询姓名中有小的名字 select * from stu where name like "%小%";
查询两个字的名字 select * from stu where name like "__";
查询三个字的名字 select * from stu where name like "___";
查询至少有两个子的名字 select * from stu where name like "__%";
正则查询 rlike
eg:查询姓刘的人的信息 select * from stu where name rlike "^刘.*$";
查询姓刘的 最后一个是华的信息 select * from stu where name rlike "^刘.*华$";