#我的第二十二堂云计算课# #开源数据库MySQL DBA运维实战 第3章 SQL2#

第3章 SQL2

一、DML:
1、插入数据insert:
(1)完全插入:inert into 表名 values (值1,值2,…);
(2)部分插入:inert into 表名(列名1,列名2) values (值1,值2);
2、更新数据update:
语法:update 表名 set 列名=值 where 约束 ;
示例:
(1)准备一张表:
create table t2 (id int ,name varchar(20));
insert into t2 values (1,aa);
insert into t2 values (2,bb);
(2)更新数据(把bb改为cc):
update t2 set name=‘cc’ where id=2 ;
(3)查询结果:select * from t2;
3、删除数据delete:
语法:delete from 表名 where 约束 ;
示例:目的:删除id为2的用户记录
delet from t2 where id=2 ;
二、DQL(mysql查询,前提是在库里面):
1、简单查询:
(1)查看所有的列:select * from 库名.表名
(2)查看部分列:select 列1,列2 from 库名.表名
(3)通过四则运算查询:select name,salary,salary*14 from employee5 // 查看年薪
2、条件查询:
(1)单条件查询where:select name,post from employee5 where post=‘hr’ //查看部门为hr的员工姓名
(2)多条件查询and/or:
select name,salary from employee5 where post=‘hr’ and salary>1000 ; //查询部门为hr的员工,且工资大于1000
select name,salary from employee5 where salary=6000 or salary=8000 ; //查询部门为hr的员工,且工资是6000或者8000的员工
(3)关键字between and 在什么之间:
select name,salary from employee5 where salary between 6000 and 8000 ; //查询部门为hr的员工,且工资在5000到15000的员工
select name,salary from employee5 where salary not between 6000 and 8000 ; //查询部门为hr的员工,且工资不在5000到15000的员工
(4)关键字IN集合查询:
select name,salary from employee5 where salary in (4000,5000,9000) //查询部门为hr且工资可能是4000,也可能是5000,还有可能是9000的员工
select name,salary from employee5 where salary not in (4000,5000,9000) //查询部门为hr且工资不是4000,也不是5000,还不是9000的员工
(5)关键字IS NULL:
select name,job-description from employee5 where job-description is null; //空
select name,job-description from employee5 where job-description is not null; //非空
select name,job-description from employee5 where job-description=‘’; //空格
(6)关键字LIKE模糊查询:
select * from employee5 where name like ‘al%’ //通配符’%’代表多个任意字符
select * from employee5 where name like ‘al_’ //通配符’_’代表多个任意字符
3、查询排序:
(1)例如以工资升序为例:select * from 表名 order by 工资的列名 ase ;
(2)例如以工资降序排列:select * from 表名 order by 工资的列名 desc;
(3)工资最高的前五名:select * from 表名 order by 工资的列名 desc limit 5 ; // 默认初始位置为0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值