insert into 表名 values(列,列),(列,列),(列,列).......
insert into 表名(列名,列名,列名.......) values (值,值,值........)
例如:create table student (id int , name varchar(20));
insert into student values (1,`zhangsan`);
可以一次插入多组数据
小tip(套娃操作):insert into 表1(列名) values select * from 表2//把查询的结果和插入语句联合到了一起(后半句查询出来的表2要和表1的结构匹配)
注:①插入的数据要与表的列数相同(否则会报错)
但有特殊写法可以让列数不相同:如insert into student(id) values(4);
②插入的数据要与表的范例相同(否则会报错)
③插入的数据范例如果是datatime范例
insert into student2 values(1 , `张三` , 2023-10-30 18:32:00);
四:查
1:查询数据 :select查询
注:所有的select 操作都是在临时表中进行的
(1)全列查询:
select * from 表名;
注:①* 通配符
(2)指定列查询:
select 列名,列名........ from 表名;
(3)表达式查询:
如查询所有门生的英语结果+10 select name ,engliish+10 from exam_student;
注:decimal(3,1)//数字长度最多是3,小数点后必须是1
(4) 给查询结果指定别名:
select 表达式 as 别名 from 表名 //让表格有更好的可读性
(5)查询去重关键字distinct:
select distinct 列名 from 表名;
(6)查询的同时进行排序:
select 列名 from 表名 order by 列名 (desc) //mysql默认升序省略了asc 关键字
注:①最后一个列名是排序的依据,列名不肯定出如今表格中,大概是几个数据相加
②MySQL默认是升序(asc)
③ desc // 关键字 descand 降序 轻易跟desc ->describe 检察表的结构产生分歧
④列名的范例也可以是字符串,字符串也能比较大小,我们期望按照中文的拼音或者笔画来进行排序,但是mysql不支持这样的功能
⑤order by 在排序的时候也有“优先级”的区别,如果第一个比较的东西如(order by math ,chinese)math的数值相同,那就比较chinese的大小 (7)聚合查询:行操作
“1”:select sum(chinese) from exam_result;
chinese那一列全部相加,sum()函数中null会忽略掉,但在SQL中null与其他数字进行运算时,结果一样平常都是null。
“2”:select sum(name) from exam_result;
sql把字符串当做数字进行算术运算,都会实验把字符串转化为数字,但是汉字不行
警告,但是不影响程序能继承跑
③:min()
查询整个表里语文结果最低的同学 :select min(chinese) from exam_result;
等价写法:select chinese from exam_result where chinese not null order by limit 1;
④:avg() 和 gruop by
误区:没有“使用聚合的列”也可以和聚合函数完的列成为同一行数据
例如,按照职业计算其平均薪资
select role,avg(salary) from emp group by role; //这里的显示次序不能认为是否存在任何次序
⑤having
分组之前(group by)的条件用where 来表现 , 分组之后用having来表现,用法同where
"1":select role , avg(salary) from emp where name != '张三' gruop by role; //句意:把“张三”剔除后,各职业的平均薪资(分组前剔除)
“2”:select role avg(salary) from emp where name != '张三' group by role having avg(salary) < 20000; //句意:去除'张三'后,分组后,平均薪资程度低于20000的role
应用:
①where + 比较运算符 : select name , chinese+math+english from exam_result where chinese + math + english <200;
②变式错误写法 select name , chinese + math + english as total from exam_result where total<200;
错误缘故原由:实行次序优先级,先实行where后的语句,在实行 chinese + math + english as total
在mysql中,让别名被界说先实现,后实行where语句不行
(4)between s1 and s2:
①应用:查询chinese 在80-90这个分段的人(也可以用 and去连接)
select * from exam_result where chinese between 80 and 90;
(5)in(option , .....)
注:如果是option中的一个,返回true
①应用:查数学结果为58或59或98或99的人(也可以用or去连接)
select * from exam_result where math in(58,59,98,99);
(6)like关键字
①%匹配恣意个字符
select * from exam_result where name like '%孙'; // 找以名字孙末了的同学, '孙%' 找以孙开头的同学 '%孙%' 找包含孙的
②_匹配一个字符
select * from exam_result where name like '孙_';//一个_
select * from exam_result where name like '孙__';//两个_
select * from exam_result where name like '_孙_';//包含
select * from exam_result where name like '__孙';//尾部
3:逻辑运算符
(1)and 和 or
例句:①select * from exam_result where chinese > 80 and english > 90;
②select * from exam_result where chinese > 80 or english >90;
注:and 优先级高于 or
例:select * from exam_result where chinese > 80 or math >70 and english >70;
实际写代码,按照逻辑去加(),显示指定的次序,不易堕落
优化后:select * from exam_result where (chinese > 80 or math > 70) and english > 70;
4:分页查询
如果数据量过大,select * 操作较危险, 引入limit
(1):limit
①select * from exam_result limit 3; // 返回前三条数据
(2):offset(默认从第0条开始)
①:select * from exam_result limit 3 offset 3 // 从第四条数据开始往后获取共计三条数据
五:改(危险操作)
1:修改 update
update 表名 set 列名 = 值 where 条件 ;
应用:
①update exam_result set math = 80 where name = '孙悟空';//将孙悟空的数学结果修改为80
②update exam_result set math = 80 ,chinese = 70 where name = '孙悟空';//修改两科结果
③update exam_result set math = math + 30 order by chinese + english + math limit 3;
//85.0+30 = 115.0 decimal(3,1) 三位数字,小数点后一位,超出范围了,所以实行失败,导致整条sql语句都实行失败