MySQL基本操作_NO.-LL的博客-CSDN博客
强烈建议先看基本操作
目录
增删改数据
添加数据(INSERT)
1). 给指定字段添加数据
2). 给全部字段添加数据
3). 批量添加数据
修改数据(UPDATE)
删除数据(DELETE)
查询数据
基本查询(不带任何条件)
1). 查询多个字段
2). 字段设置别名
3). 去除重复记录
条件查询
常用的比较运算符
常用的逻辑运算符
1、BETWEEN ... AND ... 在某个范围之内(含最小、最大值)
2、IN(...) 在in之后的列表中的值,多选一
3、LIKE 占位符 模糊匹配(_匹配单个字符, %匹配任意个字符)
4、IS NOT NULL 不是NULL
聚合函数
分组查询
排序查询order by
分页查询limit
结合练习:
INSERT INTO 表名 (字段名1, 字段名2, ...) VALUES (值1, 值2, ...);
INSERT INTO 表名 VALUES (值1, 值2, ...);
INSERT INTO 表名 ( 字段名 1, 字段名 2, ...) VALUES ( 值 1, 值 2, ...), ( 值 1, 值 2, ...), ( 值1, 值2, ...) ; //选择性批量添加
INSERT INTO 表名 VALUES ( 值 1, 值 2, ...), ( 值 1, 值 2, ...), ( 值 1, 值2, ...) ; //全批量添加
案例:
//创建表
create table emp(
id int comment '编号',
workno varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
entrydate date comment '入职时间'
) comment '员工表';
insert into emp(id,workno,name,gender,age,idcard,entrydate) values(1,'1','Itcast','男',10,'123456789012345678','2000-01-01');
insert into emp values(2,'2','Itcffffst','男',12,'123456789012345678','2000-01-01');
insert into emp values(3,'3','韦一笑','男',38,'123456789012345670','2015-01-01'),
(4,'4','赵敏','女',18,'123456789012345670','2015-01-01');
select *from emp;
UPDATE 表名 SET 字段名 1 = 值 1 , 字段名 2 = 值 2 , .... [ WHERE 条件 ] ;
update emp set name = '小昭' , gender = '女' where id = 1;
update emp set entrydate = '2022-06-01';
select *from emp;
DELETE FROM 表名 [ WHERE 条件 ] ;
delete from emp where gender = '女' and id=1;
select *from emp;
#delete from emp;
查询的执行顺序
先创建一个表方便查询
drop table if exists emp;
create table emp( id int comment '编号',
workno varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(18) comment '身份证号',
workaddress varchar(50) comment '工作地址',
entrydate date comment '入职时间'
)comment '员工表';
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (1, '00001', '柳岩666', '女', 20, '123456789012345678', '北京', '2000-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (2, '00002', '张无忌', '男', 18, '123456789012345670', '北京', '2005-09-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (3, '00003', '韦一笑', '男', 38, '123456789712345670', '上海', '2005-08-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (4, '00004', '赵敏', '女', 18, '123456757123845670', '北京', '2009-12-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (5, '00005', '小昭', '女', 16, '123456769012345678', '上海', '2007-07-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (6, '00006', '杨逍', '男', 28, '12345678931234567X', '北京', '2006-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (7, '00007', '范瑶', '男', 40, '123456789212345670', '北京', '2005-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (8, '00008', '黛绮丝', '女', 38, '123456157123645670', '天津', '2015-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (9, '00009', '范凉凉', '女', 45, '123156789012345678', '北京', '2010-04-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (10, '00010', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (11, '00011', '张士诚', '男', 55, '123567897123465670', '江苏', '2015-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (12, '00012', '常遇春', '男', 32, '123446757152345670', '北京', '2004-02-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (13, '00013', '张三丰', '男', 88, '123656789012345678', '江苏', '2020-11-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (14, '00014', '灭绝', '女', 65, '123456719012345670', '西安', '2019-05-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (15, '00015', '胡青牛', '男', 70, '12345674971234567X', '西安', '2018-04-01');
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate) VALUES (16, '00016', '周芷若', '女', 18, null, '北京', '2012-06-01');
准备完毕后,我们就可以看到emp表中准备的16条数据。接下来,我们再来完成DQL语法的学习
SELECT 字段1, 字段2, 字段3 ... FROM 表名 ;
SELECT * FROM 表名 ;
注意 : * 号代表查询所有字段,在实际开发中尽量少用(不直观、影响效率)。
SELECT 字段 1 [ AS 别名 1 ] , 字段 2 [ AS 别名 2 ] ... FROM 表名 ;
SELECT 字段 1 [ 别名 1 ] , 字段 2 [ 别名 2 ] ... FROM 表名 ;
SELECT DISTINCT 字段列表 FROM 表名 ;
select name,workno,age from emp;
select id ,workno,name,gender,age,idcard,workaddress,entrydate from emp;
# select * from emp;
# 起别名
select workaddress as '工作地址' from emp;
-- as可以省略 select workaddress '工作地址' from emp;
# 查询公司员工的上班地址有哪些(不要重复)
select distinct workaddress '工作地址' from emp;
SELECT 字段列表 FROM 表名 WHERE 条件列表 ;
select * from emp where age >= 15 && age <= 20;
select * from emp where age >= 15 and age <= 20;
select * from emp where age between 15 and 20;
select * from emp where age = 18 or age = 20 or age =40;
select * from emp where age in(18,20,40);
select * from emp where idcard like '%X';
select * from emp where idcard like '_________________X';
select * from emp where idcard is not null;
SELECT 聚合函数(字段列表) FROM 表名 ;
select count(*) from emp; -- 统计的是总记录数
select count(idcard) from emp; -- 统计的是idcard字段不为null的记录数
select sum(age) from emp where workaddress = '西安';
SELECT 字段列表 FROM 表名 [ WHERE 条件 ] GROUP BY 分组字段名 [ HAVING 分组后过滤条件 ];
where 与 having 区别
- 执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤。
- 判断条件不同:where不能对聚合函数进行判断,而having可以
select gender, avg(age) from emp group by gender ;
select workaddress, count(*) address_count from emp where age < 45 group by workaddress
having address_count >= 3;
SELECT 字段列表 FROM 表名 ORDER BY 字段 1 排序方式 1 , 字段 2 排序方式 2 ;
排序方式ASC : 升序 ( 默认值 )DESC: 降序
select * from emp order by age asc;
select * from emp order by age;
select * from emp order by entrydate desc;
select * from emp order by age asc , entrydate desc;
注意:优先满足条件一,之后再二三....
SELECT 字段列表 FROM 表名 LIMIT 起始索引 , 查询记录数 ;
- 起始索引从0开始,起始索引 = (查询页码 - 1)* 每页显示记录数
- 分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMIT。
- 如果查询的是第一页数据,起始索引可以省略,直接简写为 limit 10
select * from emp limit 0,10;
select * from emp limit 10;
select * from emp limit 10,10;
select * from emp where gender = '男' and age between 20 and 40 order by age asc ,
entrydate asc limit 5 ;