天天看點

mysql資料庫操作(三)

如需用我的練習表,發消息qq:[email protected]

日期函數 流程控制函數 系統函數

#三、日期時間

#now 傳回目前系統日期+時間
select now();

#curtime 傳回目前時間,不包含日期
select curtime();

#可以擷取指定的部分:年、月、日、小時、分鐘、秒
select year(now()) 年;
select year('1996-1-1') 年;

select year(hiredate) 年 from employees;

select month(now()) 月;
select monthname(now()) 月;

select day(now()) 日;

#str_to_date 将字元通過指定的格式轉換成日期

select str_to_date('1998-3-2','%Y-%c-%d') as out_put;

#查詢入職日期為1992-4-3的員工資訊
select * from employees where hiredate > '1992-4-3'; 
select * from employees where hiredate = str_to_date('4-3 1992','%c-%d %Y');

#date_format 将日期轉換成字元
select date_format(now(),'%y年%m月%d日') as out_put;

#查詢有獎金的員工名和入職日期(xx月/xx日 xx年)
select last_name,date_format(hiredate,'%m月/%日 %y年') 入職日期
from employees
where commission_pct is not null;
select * from employees;

#k四、其他函數(系統函數)

select version();

select database();

select user();

#五、流程控制函數

#1. if函數 if else 的效果

select if(10<5,'大','小');

select last_name,commission_pct,if(commission_pct is null,'沒獎金,呵呵','有獎金,嘻嘻') 備注
from employees;

#2.case函數的使用一: switch case 的效果

案例:查詢員工的工資,要求
	部門号=30,顯示的工資為1.1倍
	部門号=40,顯示的工資為1.2倍
	部門号=50,顯示的工資為1.5倍
	其他部門,顯示原工資

select salary 原始工資,department_id,
case department_id
when 30 then salary*1.1
when 40 then salary*1.2
when 50 then salary*1.3
else salary 
end as 新工資
from employees;





#案例:查詢員工的工資級别
如果工資大于>20000,顯示A級别
如果工資大于>15000,顯示B級别
如果工資>10000,顯示C級别
否則,顯示D級别

select salary,
case
when salary>20000 then 'A'
when salary>15000 then 'B'
when salary>10000 then 'C'
else 'D'
end as 工資級别
from employees;