✅作者简介:C/C++领域新星创作者,为C++和java奋斗中
✨个人社区:微凉秋意社区
🔥系列专栏:MySql一点通
📃推荐一款模拟面试、刷题神器👉注册免费刷题
🔥前言
本文将承接前两篇MySQL专栏的博文,讲解数据库的增删改查操作,这里的查询确切的说应该是初级的查询,不涉及函数、分组等模块,当然更深层次的查询操作将放到后面的博文中,所以小伙伴们可以关注专栏,持续学习MySQL数据库。
insert into 表名(列1,列2,...) values(值1,值2,...)
insert into 表名 values(值1,值2,...)
null
insert into t_person VALUES(null,'wangwu',32,'男',150.9')
自增列设置方法:
create table if not exists t_person(
p_id int primary key auto_increment,-- 主键 自增
...
)
关键字:auto_increment
语法:delete from 表名 [where条件]
- 删除所有数据,例如:
delete from t_person
- 删除指定数据,例如:
delete from t_person where p_id=8
语法:truncate table 表名
通过表截断(truncate
)的方式删除数据要优于使用delete,原因:
语法:update 表名 set 列名1=新值,列名2=新值,... [where 条件]
- 操作整张表,例如:
update t_person set age=18
- 操作部分数据,例如:
update t_person set age=28 where p_id=1
第一个例子的含义是把t_person表中所有的age属性改为18,第二个含义是只把p_id为1对应的age改为28
查询是数据库基础的重点,拿小本本记上
select * from 表名
select 列名1,列名2,... from 表名
select 列名1 as 别名1,列名2 as 别名2,... from 表名
select 列名1 别名1,列名2 别名2,... from 表名
别名使用示例:
SELECT employee_id as 员工编号,salary as 工资 from employees
SELECT employee_id 员工编号,salary 工资,first_name,last_name from employees e
select 列名+数字,列名-数字,列名*数字,列名/数字,列名%数字 from 表名
select distinct 列名 from 表名
select distinct 列名1,列名2,... from 表名
select 列名1,列名2,
case
when 条件1 then 结果2
when 条件2 then 结果2
...
else 其他结果
end
from 表名
使用示例:
查询员工id及其工资,并对工资进行评级:工资>10000 高薪,工资>8000 中级,工资>5000 低级,其他 底层
select employee_id,salary,
case
when salary>10000 then '高薪'
when salary>8000 then '中级'
when salary>5000 then '低级'
else '底层'
end as '薪资等级'
from employees
describe 表名
desc 表名
语法:
select 列名 from 表名 where 条件
单条件查询
查询工资>10000的员工信息:
SELECT * from employees where salary>10000
select * from employees where first_name='STEVEN'
select * from employees where binary first_name='STEVEN'
多条件查询
多个条件之间须通过and
或者or
进行拼接:
and:代表并且,多个条件同时满足,相当于java中的&&
or:代表或者,满足任意一个即可,相当于java中的||
区间查询
between 最小值 and 最大值
not between 最小值 and 最大值
枚举查询
列名 in(值1,值2,值3,...)
select * from employees where employee_id=100 or employee_id=105 or employee_id=110
select * from employees where employee_id in(100,105,110)
列名 not in(值1,值2,值3,...)
select * from employees where employee_id not in(100,105,110)
空值查询
列名 is null
列名 is not null
select * from employees where commission_pct is null
select * from employees where commission_pct is not null
模糊查询
语法:
where 列名 like '值'
%
:代表不固定长度,可以为0-n个字符
_
:代表一个长度
示例:
查询first_name中包含s的员工信息
select * from employees where first_name like '%s%'
查询firstname中以s开头的员工信息
select * from employees where first_name like 's%'
查询firstname中以s结尾的员工信息
select * from employees where first_name like '%s'
查询firstname中第二个字母为s的员工信息
select * from employees where first_name like '_s%'
对查询结果进行指定规则的排序显示
1、单列排序
select 列名 from 表名 order by 列名 asc(升序)|desc(降序)
示例:
根据工资从高到低显示员工信息
select * from employees order by salary desc
根据工资从低到高
select * from employees order by salary asc
select * from employees order by salary
tips: 默认为升序排列
2、多列排序
order by 列名1 asc|desc , 列名2 asc|desc,...
示例:
根据工资从低到高显示员工信息,如果工资相同,根据员工id从高到低显示
select * from employees order by salary asc,employee_id desc
3、where+order by
select 列名 from 表名
where 筛选条件
order by 排序条件
示例:
查询工资>10000的员工信息,从高到低显示
select * from employees where salary>10000 order by salary desc
写在最后:
查询操作前面目录前我加上了*表示其为重点内容,下一篇博客是关于查询操作的进阶,期待你的持续关注哦~