本文主要是介绍Mysql03增删改查,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Sql – DML、DQL
DML 数据操作语言
insert - 插入数据
update - 修改数据
delete - 删除数据
DQL 数据查询语言
select
insert - 插入数据
insert into tb2 values(值1, 值2, 值3)
insert into tb2(字段3,字段2) values(值3,值2)
insert into tb2 values(…),(…),(…)
将一个查询的结果,插入到一个表中
Insert into tb2 select * from …
另一种批量插入数据方式:
将查询结果,直接创建成一张新表
create table tb3 as select …. from 表名
单引号:两个单 引号转义成一个单引号
Insert into kecheng(name) values(‘I’'m ')
Sql 注入攻击
…where … password=‘1’ or ‘1’=‘1’
阻止 sql 注入攻击:
将用户填写的内容中的单引号,替换成两个单引号
…where … password=‘1’‘or ‘‘1’’=’‘1’
update – 修改数据
update tb2 set 字段1=值1, 字段2=值2, 字段3=值3 where …
update tb2 set … where …
order by … limit 5
对指定字段进行排序后,
只修改前5条数据
delete – 删除数据
delete from tb2 where …
delete from tb2 where …
order by … limit 5
按指定字段排序,并是删除前 5 行
清空表:
delete from tb2;
truncate table tb2;
select - 查询数据
select * from tb2;
select 字段3, 字段1 from tb2;
where 子句
过滤条件
= 等值过滤
<> 不等过滤
= < <= 比较大小
between … and … >= … and <= …
in(3,78,2,6,1,8) 从指定的一组值中取值
like 字符串模糊查询
% 统配多个字符
_ 统配单个字符
\ 转义符,_ %
not not between and
not in
not like
and 并且
or 或者
is null
is not null null 值过滤
distinct
去除重复数据
select distinct job_id from 表名
查询所有job_id的值,去除重复值
select distinct 字段1,字段2 from ….
去除两个字段组合的重复值
只在字段列表前出现一次
order by 子句
排序
order by 字段1
order by 字段1,字段2
先按字段1排序,字段1相同时按字段2排序
asc 升序(默认可以不写)
desc 降序
order by 字段1 desc
order by 字段1 desc,字段2 asc
select …(字段名)from…(表名)order by…
select …(字段名)from…(表名)where…order by…
字段别名
查询的字段,或表达式结果,可以起别名
select first_name as fn, salary from …
select salary*12 as s from ….
As 可以省略
where 后面不能使用字段别名
oder by 可以引用别名
select查询结构
select
distinct
from
where
order by
练习
-
查询结果插入表
– 新建tb2表
use test;
drop table if exists tb2;
create table tb2(
id int primary key auto_increment,
name varchar(20)
)engine=innodb charset=utf8;
-- 查询学生姓名,插入 tb2 表Insert into tb2(name) select name from xuesheng;-- 将 tb2 表的数据查询出来,再次插入 tb2Insert into tb2(name) select name from tb2;
-
update 测试
update xuesheng set age=age+1,banji_id=3
where name=‘王五’;
update xuesheng set age=age+1,banji_id=3
where name=’ 1’ or ‘1’=‘1’; – sql注入
– 年龄最大的2个学生,班级修改成4
update xuesheng set banji_id=4
order by age desc limit 2;
– desc 降续
– limit 2 前两条
-
准备测试数据
d:\hr_mysql.sql
source 命令:执行sql脚本文件
source d:\hr_mysql.sql 末尾不加分号
show databases; hr库
show tables;
-
查询员工表
Select * from employees;
Select
employee_id,
first_name,
salary
from
employees;
-
薪水大于等于10000的员工
select
employee_id,
first_name,
salary
from
employees
where
salary>=10000;
-
工种代码(job_id)是 IT_PROG 的员工
select
employee_id,
first_name,
salary,
job_id
from
employees
where
job_id=‘IT_PROG’;
– job_id=‘it_prog’; 大小写不敏感
-
上司工号(manager_id)是 100 的员工(100的下属)
select
employee_id,
first_name,
salary,
manager_id
from
employees
where
manager_id=100;
-
部门编号(department_id)是 30 的员工(查询30部门的员工)
select
employee_id,
first_name,
salary,
department_id
from
employees
where
department_id=30;
-
不在 50 部门的员工
select
employee_id,
first_name,
salary,
department_id
from
employees
where
department_id<>50;
- 查询员工 200
select
employee_id,
first_name,
salary
from
employees
where
employee_id=200;
- 查询 1995-1-1年之前入职
select
employee_id,
first_name,
salary,
hire_date
from
employees
where
hire_date<‘1995-1-1’;
- 工资小于 3000
select
employee_id,
first_name,
salary
from
employees
where
salary<3000;
- 薪水范围[5000,8000]
select
employee_id,
first_name,
salary
from
employees
where
salary between 5000 and 8000;
– salary>=5000 and salary<=8000;
- 10 , 20 , 60 , 90 四个部门中所有员工 ( in 过滤)
select
employee_id,
first_name,
salary,
department_id
from
employees
where
department_id in(10,20,60,90);
- 查询 first_name 中包含 ar 的员工
select
employee_id,
first_name,
salary
from
employees
where
first_name like ‘%ar%’;
- 查询 first_name 第三个字符是 e 的员工
select
employee_id,
first_name,
salary
from
employees
where
first_name like ‘__e%’;
- 查询工种(job_id)以 SA 开头的员工
select
employee_id,
first_name,
salary,
job_id
from
employees
where
job_id like ‘sa%’;
- 姓名中有 en ,且在80 部门中的员工
select
employee_id,
first_name,
last_name,
salary,
department_id
from
employees
where
(first_name like ‘%en%’
or last_name like’%en%’)
and department_id=80;
- 部门 90 中所有员工 ,和工种后缀是 CLERK 的员工
select
employee_id,
first_name,
salary,
department_id,
job_id
from
employees
where
department_id=90 or
job_id like ‘%CLERK’;
- 工资小于3000 和 工资大于15000
select
employee_id,
first_name,
salary
from
employees
where
salary not between 3000 and 15000;
– salary<3000 or salary>15000;
- 没有上司的人
manager_id是null值
select
employee_id,
first_name,
salary,
manager_id
from
employees
where
manager_id is null;
- 没有部门的员工
department_id 是null
select
employee_id,
first_name,
salary,
department_id
from
employees
where
department_id is null;
- 有提成的员工
commission_pct不是null值
select
employee_id,
first_name,
salary,
commission_pct
from
employees
where
commission_pct is not null;
- 查询 job_id
select distinct job_id from employees;
- 查询 department_id,过滤 null值
select distinct department_id from employees
where department_id is not null;
- 查询 manager_id,过滤 null值
select distinct manager_id from employees
where manager_id is not null;
-
查询 department_id, manager_id 组合,过滤 null值
select distinct department_id, manager_id
from employees
where department_id is not null
and manager_id is not null; -
薪水从小到大排列
select
employee_id,
first_name,
salary
from
employees
order by
salary; -
按部门降序排列,相同部门中,按薪水升序排列
select
employee_id,
first_name,
salary,
department_id
from
employees
order by
department_id desc, salary; -
薪水大于等于 10000 的员工,按姓名排序
select
employee_id,
first_name,
last_name,
salary
from
employees
where
salary >=10000
order by
first_name, last_name;
- 查询所有员工,按部门编号升序排列
相同部门,按first_name升序排列
select
employee_id,
first_name,
salary,
department_id
from
employees
order by
department_id, first_name;
-
查询 50 和 80 部门的员工,
按工资降序排列,工资相同按工种代码排列
select
employee_id,
first_name,
salary,
department_id,
job_id
from
employees
where
department_id in(50,80)
order by
salary desc, job_id; -
姓名连接起来显示
select
employee_id id,
concat(first_name, ’ ', last_name) name,
salary
from
employees; -
查询员工id,first_name,月薪,年薪
过滤年薪大于等于 100000,按年薪排序
select
employee_id,
first_name,
salary,
salary12 s
from
employees
where
salary12>=100000 – 不能使用字段别名
order by s; – 可以使用字段别名
这篇关于Mysql03增删改查的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!