MySQL学习记录(Day2)

DML

数据库管理语言,对数据增删改

一、新增

1.指定列添加数据

insert into staff(id,code,name,salary)VALUE(2,'1002','宣行琮',9000);

2.自动递增列可以不指定

昨天将id设为主键同事将他设为自增

insert into staff (code,name,salary)VALUES('1003','何容与',10000),
('1004','葵',8500);

自动递增列不会回撤,不会补齐  从该列最大值递增

3.not null列必须要指定数据

昨天将code,name属性设为not null,所以在添加数据时,其余可以不写,但是这两个必须要写

insert into staff (code,name)VALUES(1008,'风祭莲');

4.可以不指定列,但是需要将全部列指定数据

insert into staff VALUE(12,'10012','阿刃',81300);

二、删除数据

1.删除数据 delete

删除id为1的职员

delete from staff where id=1;

2.清空表

删除掉每一行数据

delete from staff;

清空表  性能更快一些

truncate staff;

三、修改编辑 update

update staff set name='花尘' where id=5;

update staff set salary=salary+2000 where name='宣行琮';

update staff set name='玉泽',salary=salary-2000 where id=12;

将id为5的职员名字改为花尘

将职员名字为宣行琮的职员的公职加2000

将id为12的职员的名字改为玉泽,并将工资减2000

DQL

查询数据

select 1;
select now();-- 时间

一、查询表格

1.指定列名查询

select name,salary from staff;

查询staff表中的职员姓名和工资信息

2.使用*代替所有的列

select * from staff;

3.可以使用as制定别名  列、方法结果、表、视图

SELECT name as `姓名` from staff;

将name属性改为姓名

4.where指定条件语句

select * from staff where id=4;

查询id为4的职员信息

(1)不等于
select * from staff where id!=1;
select * from staff where id<>1;
(2)大于小于
select * from staff where id>=3;
select * from staff where id<=3;
(3)对null值的判断
select *from staff where salary is null;
select *from staff where salary is not null;-- 非空
-- 等于null
select *from staff where salary <=> null;
(4)多条件

and    or

select*from staff where id<100 and salary>=10000;
select * from staff where salary>=10000 or salary<=500;

in     not in 

select * from staff where id=1 or id=3.....
select * from staff where id in(1,3,5,7,8,10,12);
select * from staff where id not in(1,3,5,7,8,10,12);

BETWEEN  AND

select * from staff where  salary BETWEEN 5000 and 10000;
(5)模糊查询

% 任意个数任意字符  _有且只有一个字符

-- 名字里带何字的
select * from staff where name like '%何%';
-- 查询姓张的
select * from staff where name like '花%';
-- 宣某某
select * from staff where name like '宣__';

5.是否存在  exists

select *from staff where exists (select *from staff where 1=1);

只有后面的条件成立时,前面才可以查询,但是后面对前面的输出内容没有影响,只是一个控制条件

6.any    ALL 

select * from staff where salary > any(select code from staff );
select * from staff where salary > all(select code from staff);

any  只要满足其中一个条件就可以

all   必须满足全部的条件

7.排序  order by

select * from staff order by salary ;

按照工资的多少来排序,如果没有说就是正序,从小到大

正序 asc默认正序    倒序 desc

select * from staff order by salary asc;
select * from staff order by salary desc;
update staff set code='1009' where name='玉泽';
select * from staff order by salary  asc,code desc;

最后一个首先先按工资的正序排列,如果工资相同就按编号的倒序排列。

8.去重 对整个查询结果去重

如果工资相同时,就会比较名字,若名字不相同,就不会去重

select distinct salary,name from staff ;

9.部分查询 limit

select * from staff order by salary desc limit 3;

查询倒序排列数据的前三个

10.查询第三名到第五名 limit start,count;

select * from staff order by salary desc limit 2,3;

倒序排列后,从下标为2的开始,一共取三组数据

11.case when then

select *,case 
when salary<10000 then '薪资较低'
when salary >=10000 and salary <=10500 then '薪资正常'
else '薪资较高'
end
from staff;


select *,case salary when 8500 then '还行'
when 10000 then '还可以'
when 11000 then '很高'
end as `level` from staff;

两种方法,在全部信息之后会添加一列用来描述工资的情况

二、拼接(合并)查询结果

select name,salary from staff UNION
select code,name from staff ;

将staff中name、salary属性和staff中code、name属性拼接在一起

1.union 会去重

当表中有一组完全一样的数据时,在拼接时,union会自动去掉

select name,salary from staff UNION
select name,salary from staff;

虽然拼接的是两个表,但是只会显示一个表,因为union自动去重了

2.Union ALL

select name,salary from staff UNION ALL
select name,salary from staff;

加上ALL 即使两个表一模一样union也不会去重了

三、分组

聚合函数  将多个数据聚合成一个数据的函数

1.5个聚合函数

-- 最大值
select max(salary) from staff;
-- 最小值
select min(salary) from staff;
-- 平均数
select avg(salary) from staff;
-- 求和
select sum(salary) from staff;
-- 求个数
select count(salary) from staff;

2.分组查询 group  by

select department,avg(salary) as 平均薪资 ,sum(salary) as 薪资总和 from staff GROUP BY department;

通过department来分组

3.分组筛选 having

having 对分组之后对分组之后的数据再筛选

select department from staff
group by department 
having avg(salary)<10000;

4.null值

五个聚合函数都会忽略null值

count(*)这样不会忽略null值

四、连接查询

-- 老师的姓名以及教授课程名称
-- teacher tname属性 course cname属性
-- teacher.tid=course.tid
select tname,cname from teacher LEFT JOIN
course on teacher.tid=course.TId;
-- as 起别名
select a.tname, b.cname from teacher as a left join course as b on a.tid=b.tid;

通过两个表的相同元素来连接查询

1.外连接

左外连接left join 

左连接以左表为主表,会显示所有的数据,右表为附表,只会显示与左表有关系的数据

右外连接 right join

右连接以左表为主表,会显示所有的数据,左表为附表,只会显示与右表有关系的数据

全外连接  full join

2.内连接

 inner join  也可以写为join

只显示有对应关系的数据

若其中有一个表内容为null值就不会显示

3.交叉连接 cross join

select *from teacher a,course b where a.tid=b.tid;

4.子查询

select sname from student where sid in(select sid from sc where score<60);

就是嵌套查询,查询不及格同学的姓名

select sname from (select * from student where sid=01) as b;

查询学生id为01的学生的姓名

  • 13
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值