MySQL表的增删改查
程序员文章站
2022-05-07 22:41:09
...
create database test character set utf8mb4;
例如这里我们加一个学生表
create table student(
sn int,
name varchar(20),
accout decimal(11,2),
sgroup int
);
1.新增(insert into … values …)
插入单行数据语法:
insert into student(sn,name,accout,sgroup) values
(1,'小文',100.50,3);
插入多行数据,用逗号分隔
insert into student(sn,name,accout,sgroup) values
(2,'小豪',50.50,3),
(3,'小李',88.88,3),
(4,'小宜',28.88,4);
2.查询操作(select/from)
//*号代表所有列
select * from student;
//or
select sn,name,accout,sgroup from student;
select sn,name,accout from student;
别名(结果集表头列的别名):
select sn,name,accout+200 from student;
//表头名称也是account+200
//别名的方法
select sn,name,accout+200 acc from student;
去重
distinct 是唯一的意思
select distinct sgroup from student;
排序(order by asc/desc)
--升序:
select * from student order by accout asc;
--降序:
select * from student order by accout desc;
条件查询
例如我们这里查询账户在50以上的。
年纪为3;
--条件
select * from student where accout >50;
select * from student where sgroup=3;
select sn,name,accout+200 from student where accout+200 > 300;
--区间
select * from student where accout between 70 and 90;
--查指定属性的学生
select * from student where sn in (2,4);
--判断那一列是空
select * from student where name is null;
--判断不为空
select * from student where name is not null;
--模糊查询
select * from student where name like '%李';//什么什么李,不能是 李%
--查询多个条件(用逻辑运算符来解决)
select * from student where accout>50 and sgroup=3;
--条件与排序
select * from student where accout>50 order by accout;
逻辑运算符
运算符 | 说明 |
---|---|
AND | 多个条件都必须为TRUE(1),结果才是TRUE(1) |
OR | 任意一个条件为TRUE(1),结果为TRUE(1) |
NOT | 条件为TRUE(1),结果为FALSE(0) |
分页查询:LIMIT
--从0开始,筛选n条结果
select * from student limit 2;
--从s开始,筛选n条结果
select * from student limit 2,2;
--建议用这个
select * from student limit 2 offset 2;
select * from student where (name like '%李'or name like '%豪' ) and
(accout >50 or sgroup = 3);
修改操作
--修改小文的年纪
update student set sgroup=1 where sn=1;
--修改年纪和账户
update student set sgroup=2,accout=50 where name='小文';
update student set sgroup=3,accout=accout+80 where name='小文';
删除(Delete)
delete from student where sn=4;
上一篇: php实现文件下载代码分享_php技巧