46到49My SQL快速入门DML语句

46

4.DML语句
查询 select * from 表名;
select *from t1; 查询所有内容
select age form t1;查询指定字段age;
1)插入记录
1>插入记录
insert into 表名(字段1,字段2,字段3,…,字段n) values(值1,值2,值3,…,值n);
insert into t1(id,age) values(1,20)
insert into t1 values(1,20) 1,20要一 一对应
也可以不用指定字段名,但是values后面的顺序应该和字段的排序一致
2>一次插入多条记录
insert into 表名(字段1,字段2,字段3,…,字段n)
values
(值1,值2,值3,…,值n),
(值1,值2,值3,…,值n),
(值1,值2,值3,…,值n)
;
insert into(id,age) values (1,20),(2,30),(3,40);

2)更新记录
1>更新一个表
update 表名 set 字段1=值1,字段2=值2,…字段n=值n [where 条件];
update t1 set age=60 where id=6; //将id为6的字段改成了60
update t1 set age=60 where id=6 and age=50;//要满足id=6,还要满足age=50,才能将age改为60
2>更新多个表中数据
update 表1,表2,…表n set 表1.字段1=表达式1,表n.字段n=表达式n [where 条件];
注:多表更新更多的用在根据一个表的字段来动态的更新另外一个表的字段

简单实例:
update t1,t2 set t1.age=2000,t2.age=3000 where t1.id=1 and t2.id=1;
3)删除记录
1>删除单表中的数据
delete from 表名 [where 条件];
delete from t1 where id=1;
2>删除多个表中的数据
delete 表1,表2,…表n from 表1,表2,…表n [where 条件];
不管是单表还是多表,不加where条件将会把表中的所有记录删除,所以操作时一定要小心。
delete t1,t2 from t1,t2 where t1.id=1and wher t2.id=1

47

4)查询记录
select 字段名|* from 表名;
select *from enployee
select id,name,salary frome enployee
1>查询不重复的记录
SELECT distinct field1,field2 FROM 表名;
select distinct name,id from enployee 这样重复的还会出现因为id号不同 只要field1,field2任何一个字段有不同就会被选择!
select distinct name from enployee //把所有都显示,把重复的去掉 一般使用distinct,只筛选一个字段!
2>条件查询
注:条件字段比较符号:
=,<,>,>=,<=,!=等比较运算符
select distinct name from enployee where id>4; select distinct name from enployee 如果没有重复的和 select *from enployee是一样的

多个条件之间可以使用or and等
select distinct name from enployee where id=1 or id=3;//或
select distinct name from enployee where id>2 and id<7;//并且

where 后面接条件
select * from 表名 where 条件
3>排序和限制
排序:
asc:由低到高,也是默认值
select * from employee order by salary asc;
desc:由高到底
select * from employee order by salary desc;
多个字段排序
select * from employee order by salary desc,id desc;
限制:
在语句的最后面 加上limit 数字1,数字2 来进行查询数量的限制。
limit 数字1,数字2 数字1代表从第几条记录开启取(是从0开始的),数字2代表取几条!
select from employee order by salary desc limit 1;获取1行,获取两行就写2
select from employee order by salary desc limit 0,1;从第0条取开始取一条
4>聚合
①sum求和
select sum(字段名) from 表名;
select sum(salary) from employee;
②count记录总数
select count(
|字段名) from 表名;
select count(
) from 表名; 查询所有字段数
③max最大值
select max(字段名) from 表名;
select max(salary) from employee;
④min最小值
select min(字段名) from 表名;
select min(salary) from employee;
⑤GROUP BY分类聚合
按照某个字段分组
110组 和是多少
120组 和是多少
119组 和是多少
select sum(salary) from employee group by department;
运行结果
1500
800
789
select department,sum(salary) from employee group by department;
运行结果 110 组 1500
120 组 800
119 组 789
⑥WITH ROLLUP分类聚合后的结果进行再汇总
select sum(salary) from employee group by department with rollup;

运行结果 1500
800
579
2879

⑦HAVING
注意:having和where的区别在于,having是对聚合后的结果进行条件过滤,而where是在聚合前就对记录进行过滤
,应该尽可能的对记录进行先过滤! 聚合差不多就是和的意思
department 110
select sum(salary) from employee group by department having sum(salary)>1000;
运行结果
department sum(salary)
110组 1500
在一起使用:select sum(id),max(id),min(id),count() from a1;
select sum(salary),max(salary),min(salary),count(
) from employee;

48

5>表连接
需求:显示多个表中的字段的时候即可使用表连接
连接分类
内连接:选取两张表中相互匹配的记录
外连接:不仅仅选取两张相互匹配的记录,并且会选出其他不匹配的记录
举例:
内连接:select 表.字段,… from 表1名,表2名,… where [匹配的条件比如 表1.字段=表2.字段];
select 语句可以给字段起别名!直接写在需要查询显示的字段的后面就ok
给表起别名 SELECT *FROM enployee e,enployee_record er where e.id=er.eid;如果先表名太长可以这样写
SELECT enployee.id,enployee.name,enployee_record.record FROM enployee,enployee_record where enployee.id=enployee_record.eid;

SELECT enployee.id a,enployee_record.eid b FROM enployee,enployee_record where enployee.id=enployee_record.eid; 将enployee表中的id改为a,enployee_record的eid改为c
外连接
1)左连接
概念:包含左边表中的所有记录(包括右表中没有和它匹配的记录)
SELECT *FROM enployee LEFT JOIN enployee_record ON enployee.id=enployee_record.eid; 把左边的表都给查询出来,右边即使没有对应的也查出来
select ename,deptname from emp left join dept on emp.deptno=dept.deptno;

2)右连接
概念:包含右边表中的所有记录(包括左表中没有和它匹配的记录)
左连接和右连接是可以相互转换的!
SELECT *FROM enployee RIGHT JOIN enployee_record ON enployee.id=enployee_record.eid;

49

6>子查询
需求:一个查询需要另外一个查询的结果参与的时候
用于子查询的关键字:
in
语法:select * from employee where id in(select eid from employee_late);//查询employee表中所有符合employee_late表中eid字段的值
in 在…里面
注意点 in后面的子语句必须只返回一个字段
若查询结果唯一(只有一条)可以使用=代替in
not in
与in相反 不在那个表结果中的
exists
语法:select语句 where exists(select 语句);
exists:后面那个子语句有没有查询出记录来,如果查询出记录来返回true,否则就是false
并且查询出来的记录的具体的值是NULL也是没有关系,也是返回true.
not exits
与exists相反
1)select * from emp where deptno in(select deptno from dept);
2)若查询结果唯一可以使用=代替in
select * from emp where deptno=(select deptno from dept limit 1);
7>记录联合
我们常常会碰到需要将两个表或者多个表的数据按照一定的查询条件查询出来后,将结果合并到一起显示这是就需要用到记录联合
多个select 语句用
UNION或者UNION ALL隔开即可实现
select id,name,salary from enployee union select *from employee_late;
区别: 前者 会将多个查询结果合并后并且进行去除重复后返回
后者 则直接合并并不去除重复
联合的条件:查询的列个数要相等
select id from enployee union all select id from enployee;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值