日期转换,补充(建表、DML),简单函数,多表连接

  • binary添加在列名之前是区分大小写的意思

补充(建表、DML)

#建表
#新建一张表
create table 表名(
...
)

#子查询建表 (只继承结构和数据,约束无法拷贝)
create table emp_copy
as
select * from emp

create table emp_copy1
as
select empno 工号,ename,sal from emp where deptno = 10

#利用拷贝建一张空表
create table emp_copy1
as
select * from emp where 100 = 1000

#修改工作地点在NEW YORK或CHICAGO的员工工资,工资增加500(利用查询作为结果)
update emp set sal = sal + 500
where deptno in(select deptno from dept where loc in ('NEW YORK','CHICAGO'))

#删除工作在NEW YORK的员工记录(利用查询作为结果)
delete from emp
where deptno in(select deptno from dept where loc in ('NEW YORK'))

简单函数

#函数 函数名(参数)
#虚表
select abs(-19) from dual#虚表
select pi() from dual

#round(x,y)将x精确到小数点后y位
select round(99.456,2),round(95.554,0),round(95.554)#y如果省略,四舍五入整数
from dual

#truncate(x,y)截断,往后清零
select truncate(99.456,2),truncate(95.554,0),truncate(95.554,-1)
from dual

#字符串函数
#substr(str,pos,length) str是操作的字符串,pos位置从1开始,length是截取的长度
select substr('helloworld',1,3) from dual

#查询姓名以s开头的员工信息
select ename from emp where substr(ename,1,1) = 's'

#查询姓名正好为5个字符的员工姓名,工资,部门号
select ename,sal,deptno from emp where length(ename) = 5

select concat(substr(ename,1,1),lower(substr(ename,2))) 名字,length(ename)
from emp

#日期函数
#mysql  5.6版本以后支持日期作为默认值
create table test7(
regtime datetime default now()
)
#curdate()和current_date():获取当前日期函数
#now():返回服务器的当前时间和日期
select curdate(),current_date(),now()
from dual

#DATE_FORMAT(date,format):格式化日期
# %y--年
# %m--月
# %d--日
# %h--时24小时H
# %i--分
# %s--秒
select date_format(now(),'%y-%m-%d %h:%i:%s') from dual
#datediff(expr1,expr2):放回两个日期相减相差的天数
select datediff(curdate(),hiredate)
from emp

#extract(unit from date)提取时间,年月日
select hiredate,extract(year from hiredate)
from emp

select * from emp where extract(year from hiredate)='1981'
select * from emp where extract(month from hiredate)='5'
select * from emp where extract(day from hiredate)='17'
select hiredate, extract(hour from now()) from emp;
select hiredate, extract(minute from now()) from emp;
select hiredate, extract(second from now()) from emp;


#重点函数
#now()年月日时分秒,curdate()年月日,curtime()时分秒 返回系统当前时间
#date_format(date,format)
#datediff(大,小)

#一般函数
#SUBSTR(str,pos,length)
#ROUND,truncate

#1、查询服务器时间
select new() from DUAL
#2、查询部门10,20的员工截止到2000.1.1,工作多少个月,入职月份
select timestampdiff(month,'2019-3-5','2019-9-30')
from dual
select timestampdiff(month,hiredate,date_format(hiredate,'%m'))
from dual




-- 1.查询服务器当前时间
select now() from dual
-- 2.查询部门10,20的员工截止到2000年1月1日,工作了多少个月,入职的月份
-- TIMESTAMPDIFF(按照什么求差,日期1,日期2)
select TIMESTAMPDIFF(MONTH,hiredate,now()),EXTRACT(MONTH FROM hiredate),DATE_FORMAT(hiredate,'%m')
from emp
where deptno in (10,20)

#如果员工试用期6个月,查询职位不是MANAGER的员工姓名,
#入职日期,转正日期,入职日期是第多少月

select ename,hiredate 入职日期,date_format(hiredate,'%m') 入职月份,DATE_ADD(hiredate, INTERVAL 6 MONTH) 转正日期 from emp where job !='MANAGER' 

#流程控制函数
#case
#decode
#查询员工姓名,工资,部门编号,部门名称
select ename,sal,deptno,
(case deptno when 10 then '开发部' when 20 then '实施部' when 30 then '测试部' else '小卖部' end )dname from emp


#计算2000年1月1日到现在有多少月,多少周
select timestampdiff(month,'2000-1-1',now()),timestampdiff(week,'2000-1-1',now()) from dual

#查询ename的第三个字母是A的员工的信息
select * from emp where ename like '__A%'
#将员工工资按如下格式显示:123,234.00 RMB
select concat(sal,'RMB') from emp

#查询员工的姓名以及其经理编号,要求对于没有经理的现实“no Manager”字符串
select ename,ifnull(mgr,'No Manager') mgr from emp
#将员工的参加工作日期按如下格式显示:月份/年份
select date_format(hiredate,'%m/%Y') from emp
#在员工表中查询出员工的工资,并计算应交税款:如果工资小于1000,税率为0,
#如果工资大于等于1000并小于2000,税率为10%,如果工资大于等于2000并小于3000
#税率为15%,如果工资大于等于3000,税率为20%

SELECT sal,(case 
                        when sal<1000 then 0
                        when sal>=1000 and sal<2000 then sal*0.1
                        when sal >=2000 and sal <3000 then sal *0.15
                        else sal*0.2 end)tax
from emp
#方法二
SELECT sal,(case truncate(sal/1000,0)
                        when 0 then 0
                        when 1  then sal*0.1
                        when 2 then sal *0.15
                        else sal*0.2 end)tax
from emp

#重要排名
#重要
#date_format(date,format)
#now() curdate() curtime()

#最好记
#case xx when xx then xx
#ifnull(如果空,返回)
#timestampdiff(month|year|week,日期1,日期2)  datediff(日期1,日期2)
#date_add|date_SUB(date,interval 6 month)

#废柴
#concat
#length,lpad|rpad....

多表连接

#问题
#查询语句,查询员工姓名、部门名称、工作地点
select ename,dname,loc from emp,dept where emp.deptno=dept.deptno 

#涉及的表是一张以上,两张表就会自动发生连接
#连接规则:笛卡尔积,一张表所有记录与另一张表记录全部连接的情况
select ename,dname,loc from emp,dept where emp.deptno=dept.deptno and loc='chicago'

#注意:
#①同名列必须要加表名作为前缀;
#②只要涉及一张以上表别落下连接条件否则笛卡尔积
#③表名也可以起别名,方便写前缀

#查询所有员工编号,姓名,部门编号,工作地点
#Column 'deptno' in field list is ambiguous:列名冲突,没写前缀
select empno,ename,dept.deptno,loc from emp,dept where emp.deptno=dept.deptno

#查询工作地点在NEW YORK的员工编号,姓名,部门,工作地点
select empno,ename,d.deptno,loc from emp e ,dept d
where e.deptno=d.deptno and loc ='new york'

#写一个查询,显示所有员工姓名,部门编号,部门名称。
select ename,e.deptno,d.dname from emp e,dept d where e.deptno =d.deptno
select ename,e.deptno,d.dname 
from emp e join dept d on e.deptno = d.deptno


#写一个查询,显示所有工作在chicago并且奖金不为空的员工姓名,工作地点,奖金
select ename,d.loc,comm from emp e,dept d where e.deptno = d.deptno and comm is not null and loc = 'chicago'
select ename,d.loc,comm 
from emp e 
join dept d 
on e.deptno = d.deptno 
where  comm is not null and loc = 'chicago'


#写一个查询,显示所有姓名中含有A字符的员工姓名,工作地点
select ename,loc from emp e,dept d where e.deptno=d.deptno and ename like '%A%'
select ename,loc
from emp e
join dept d
on e.deptno = d.deptno
where ename like '%A%'

#工资等级表
#1 0-600
#2 601-1500
#3 1501-2500
#4 2501-4000
#5 4001-10000
create table salgrade(
grade int auto_increment,
losal decimal(7,2) default 0,
hisal decimal(7,2) default 0,
primary key(id)
)
#查询员工姓名,工资,奖金,工资等级
select ename,comm,grade,sal,losal,hisal
from emp
join salgrade
on sal between losal and hisal

#查询员工姓名,工资,奖金,工资等级,部门名称,要求只显示工资等级2以上的,按照工资等级排序
select ename,sal,comm,grade,dname
from emp e
join dept d
on e.deptno=d.deptno
join salgrade s
on sal between s.losal and s.hisal 
where grade > 2  
order by grade

#自连接
select e.ename,m.ename
from emp e
join emp m
on  e.mgr=m.empno


作业:查询入职时间比其直接上级入职时间早的员工信息

你可能感兴趣的:(日期转换,补充(建表、DML),简单函数,多表连接)