执行sql文件
source D:\sql文件.sql
给表添加一个字段
语法: ALTER TABLE 表名 ADD 字段名 属性;
ALTER TABLE student ADD score int;
查询当前使用的数据库
select database();
起别名
select 字段名 as 别名 from dept;
条件查询语句
3.列的数学运算
select 工资*12 from 表名;
= 等于
查看工资为800的员工名称
select name from emp where sal = 800;
<>或 != 不等于
查看信息不等一800的员工名称
select name from emp where sal != 800;
<小于
<= 小于等于
>大于
>=大于等于
between ... and ....两个值之间
查询薪资在2450在3000之前的员工名称 between and 遵循左小右大
select name from emp where 工资 between 2450 and 3000
is null 为 null (is not null不为空)
查询员工的津贴为null的
select * from emp where 津贴 is null;
查询员工的津贴不为null的
select * from emp where is not null;
and 并且
查询工作岗位为开发,并且工资大于2500
select * from emp where 岗位=‘开发’ and 工资>2500
or 或者
查询工作岗位为开发或者工资为2500的
select * from emp where 岗位=‘开发’ or 工资>2500
and和or同时使用
查询工资>2500并且部门编号为10 or 为20的
select * from emp where 工资 > 2500 and (编号 = 10 or 编号 = 20)
in 包含,相当于多个 or (not in 不在这个范围中)
查询工作岗位为 开发 or 人事的员工
select * form emp where 岗位 in (“开发”,"人事");
not not可以取非,主要用在 is 或in 中
is null
is not null
in
not in
查询工作岗位不为 开发 or 人事的员工
select * from emp where 岗位 not in ("开发","人事")
ike
like称为模糊查询,支持%下划线匹配
%匹配任意多个字符
下划线,一个下划线只匹配一个字符
找出名字中还有o的
select * from emp where name like '%O%';
结果: jones,scott,ford
找出名字以T结尾的
select * from emp where name like '%T';
找出名字为以k开始的
select * from emp where name like 'k%'
找出第二个字母为a的
select * from where name like '_A%';
找出第三个字母是R的
select * from where name like '__R%';
找出名字中带有 _ 的人 !加 \ 进行转义
select * from where name like '%\_%';
排序
#降序
select * from where emp order by 工资 desc;
#升序
select * from where emp order by 工资 asc; 默认升序
#先按照工资升序拍,薪资相同再按照名字升序拍
select * from where emp order by 工资 asc , name asc;
#找出工资在1250和3000之间按薪资降序排序
select * from where emp between 1250 and 3000 order by 薪资 desc;
单行处理的常见函数
lower() 转换小写
大写字母全部转化为小写字母
select lower(name) from 表名;
upper() 转换大写
小写字母全部转化为大写字母
select lower(name) from 表名;
substr() 取字符串长度内容
去查询名字的第一个字母 substr(被截取的字符串,起始下标,截取长度),起始下标不能为0
select substr(name,1,1) from 表名;
concat()进行字符串拼接
select concat(name,age) from 表名;
结果:小米10
length 取长度
select length(name) from 表名;
聚合函数
max()最大值
#找出工资最高的
select max(工资) from 表名;
min()最小值
#找出最低工资
select min(工资) from 表名;
avg()平均值
select avg(工资) from 表名;
sum()求和
select sum(工资) from 表名
count()计数
#查询信息的个数
select count(工资) from 表名;
分组查询
按照工作岗位分组,让后对工资求和
select job,sum(工资) from 表名 group by job;
找出 每个部门 ,不同工作岗位的最高薪资
select max(工资) from 表名 group by 部门,job
having可以对分完组之后的数据进一步过滤,having不能单独使用,having不能代替where,having必须和group by联合使用
例子:以部门编号分组,查询出部门最大工资并且大于3000的
select 编号,max(工资) from 表名 group by 部门编号 having max(sal) > 3000;
查询结果去重
distinct去重
select distinct job from 表名;
笛卡尔积现象:第一张表的名字都会和第二张表的内容去进行匹配一遍
解决方法:加上限制条件即可
内连接
SQL92语法
第一种:
select ename,dname from emp,dept where emp.deptno = dept.deptno;
第二种: 给表起别名
select ename,dname from emp e,dept d where e.deptno = dept.deptno;
SQL99语法
子表 主表
select e.ename,d.dname from emp e join dept d on e.deptno = d.deptno;
内连接之不等值连接
内连接之子连接
这是a表
这是b表
查询出该员工的领导,并输出员工名所对应的领导名
select a.ename as '员工名',b.ename as '领导名' from emp a join emp b on a.mgr = b.empno;
外连接
右外连接
right代表什么:表示将join关键字右边的这张表堪称主表,主要是为了将这张表的数据全部查询出来,捎带着关联查询右边的表。
在外连接当中,这两张表链接,产生了主次关系。
left:与上面相反
子表 主表
select e.ename,d.dname from emp e right join dept d on e.deptno = d.deptno;
emp:人员表 人员表
dept:部门表 主表
结果为
子查询 子查询可以出现在select,from,where后面,查询的都可以做为一张表
where后面的子查询
查询出工资大于最低员工的工资
select 工资 from 表名 where 工资 > (select min(工资) from 表名);
union的用法
就是合并两张表的结果,要比 in 的效率要高
select ename,job from emp where job = '开发'
union
select ename,job from emp where job = '人事';
limit的使用 limit 开始下标 条数 limit从第0条开始
将查询结果集的一部分取出
按照薪资降序,去除排在前5名的员工?
select ename,sal from emp order by sal desc limit 5;
(页码-1)* 3 通用的
插入数据语句
给那个字段插,表名后面的()就写那个字段名,其他的为null
insert into 表名(no,data,name) values(1,20,"小米");
插入日期数据
mysql的日期格式:%Y年 %m月 %d日 %h时 %i分 %s秒
长日期格式:%Y-%m-%d %h:%i:%s
短日期格式: %Y-%m-%d
str_to_date:字符串转日期类型date
insert into 表名(id,name,birth) values(1,"张三",str_to_date('01-10-1990','%d-%m-%Y'));
id:int
name:varchar
birth:date
如果写入的函数正好时字符串类型,就不用str_to_date
段日期插入date
insert into 表名(id,name,birth) values(1,"张三",'1990-01-02');
长日期插入datetime
inster into 表名(id,name,birth) values(1,"张三","199-10-02 14:48:20");
inster into 表名(id,name,birth) values(1,"张三",now()); #长日期now()获取当前时间
date_format:将日期类型转换成特点格式的字符串