Ø 单行插入
insertinto 表名(列名1,列名2,列名3……) values(值1,值2,值3……)
insertinto 表名 values(值1,值2,值3……)(给表中所有列插入数据不需要写列名)
Ø 多行插入
insertinto 表名(列名1,列名2,列名3……) values(值1,值2,值3……),(值1,值2,值3……),(值1,值2,值3……)
插入数据的顺序: 学生表/科目表à成绩表
#新增
insert into student(stuname,borndate,address) values('张三','2019-08-13','光谷')
select * from student insert into student(stuname,borndate,address) values('李四','2015-08-13',null), ('王文彦','2019-08-13','光谷')
select * from `subject` insert into `subject` values(null,'jqyery'),(NULL,'mysql')
select * from result
insert into result VALUES(null,3,1,56) |
Ø 删除
带条件的
deletefrom 表名 where 条件
不带条件的
delete from 表名;
面试题
说说delete与truncate的区别?
delete删除的时候是一条一条的删除记录,它配合事务,可以将删除的数据找回。
truncate删除,它是将整个表摧毁,然后再创建一张一模一样的表。它删除的数据无法找回。
注意:delete删除,uid不会重置!而使用truncate操作,uid会重置
delete from result where id=4 #删除 学号为1的 科目id为2的成绩记录 DELETE from result where stuno=1 and subjectno=2 #删除 学号为1的 科目id为2 成绩大于100分的记录 DELETE from result where stuno=1 and subjectno=2 and score>100 |
Ø 修改
Update 表名 set 列名=值,列名=值 where 列名=值
#修改 张三的地址为 火星 update student set address='火星' where stuname='张三' #将 2017年以后 出生的学生的 并且 姓 为 赵的学生的 地址改为大日本 update student set address='..日本' where borndate>'2017-01-01' and stuname like '赵%' |
Ø 查
语法: distinct去掉重复项
select[distinct] *| 列名,列名 from 表名 [where条件]
单表查询
#查询所有学生信息 select * from student #查询 学号和姓名 并给 列取别名 SELECT stuno 学号 ,stuname as 姓名 from student SELECT * from result #在成绩表中查询参加考试了的学生的学号 # DISTINCT 去掉重复项 SELECT DISTINCT stuno from result where score is not null #查询地址不为空的学生的姓名 #空有2种情况 null '' SELECT stuname from student where address is not null and address !='' #查询地址为空的学生的姓名 SELECT stuname from student where address is null or address =''
select * from student #按照年龄 从大到小 显示学生的所有信息 #排序 默认是升序 asc select * from student ORDER BY borndate asc #按照年龄 从小到到 显示学生的所有信息 select * from student ORDER BY borndate desc #分页显示 学生的信息 要求 每页显示2行信息 # limit 参数1,参数2 参数1 代表数据起始行的索引(从0开始) 参数2代表每页显示的行数 #第一页 select * from student limit 0,2 #第二页 select * from student limit 2,2
select * from result #分组 #在成绩表中求出每个学生考试的平均分 # avg() #分组 GROUP BY # GROUP BY不会单独使用 而是结合其他函数一起使用 #select * from result GROUP BY stuno
select stuno 学号, avg(score) 平均分 from result GROUP BY stuno #分组条件 where一般是作为查询的条件 跟在 from 后面 #分组的条件 使用having
#在成绩表中求出每个学生考试的平均分 只显示及格的信息 select stuno 学号, avg(score) 平均分 from result GROUP BY stuno HAVING avg(score)>=60
#查询可能出现的情况 select FROM where # 查询的条件 GROUP BY #对前面查询的数据作分组 HAVING #对分组后的数据做筛选 order by #排序(对查询最终版的数据作排序显示) LIMIT 0,2 #对排序好的数据分页显示
select stuno 学号, avg(score) 平均分 from result GROUP BY stuno HAVING avg(score)>=50 order by avg(score) limit 0,2 |
Ø 常见函数
Count() 求个数
count(*) 代表查询所有行的记录数
Count(列名) 查询该列不为空的记录数
#求出学生表中所有学生的人数 SELECT COUNT(*) from student |
Sum() 求和 avg() 求平均值
日期函数 DATEDIFF(日期1,日期2) 值为2个日期的天数差
求整的函数 CEILING (number2 ) //向上取整 FLOOR (number2 ) //向下取整
#打印出学生表中所有学生的姓名和年龄 SELECT stuname, CEILING(DATEDIFF(NOW(),borndate)/365) from student |
Ø 多表查询
内连接(取多个表数据的公共部分)
语法:两种写法
写法1
Select 表1.*,表2.* From 表1,表2 Where 表1.id=表2.xid(2表之间的外键关联 不加话的会形成笛卡尔集现象) 条件的个数 N个表内连接查询条件(表与表之间的关联)至少为(n-1)个
SELECT st.stuname 姓名, s.subjectname 科目名称,r.score from `subject` s,result r,student st where s.id=r.s_id and r.stuno=st.studentno |
写法2
Select 表1.*,表2.* 。。。 From 表1 Join 表2 on 表与表之间的关联 Join 表3 on 表与表之间的关联
SELECT st.stuname 姓名, s.subjectname 科目名称,r.score from `subject` s JOIN result r on s.id=r.s_id JOIN student st on r.stuno=st.studentno
|
#工具生成多表查询
SELECT student.stuname, `subject`.subjectname, result.score FROM result INNER JOIN student ON result.stuno = student.stuno INNER JOIN `subject` ON result.subjectno = `subject`.subno where score is not null |