码迷,mamicode.com
首页 > 数据库 > 详细

SQL语句之函数

时间:2020-03-19 13:12:12      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:sha   shanghai   hang   中秋   account   select   条件   search   语句   

 1 # 打印入职年份
 2 SELECT ename,DATE_FORMAT(hiredate,"%Y") AS year FROM t_emp;
 3 # 查询1981年上半年入职的员工
 4 SELECT empno,ename,hiredate FROM t_emp WHERE DATE_FORMAT(hiredate,"%Y")=1981 AND DATE_FORMAT(hiredate,"%m")<=6;
 5 # 条件函数->中秋节发放礼品,SALES部门发放A,其余部门发放B
 6 SELECT e.ename,d.dname,IF(d.dname="SALES","A","B") AS gift FROM t_emp e JOIN t_dept d ON e.deptno=d.deptno ORDER BY d.deptno;
 7 # SALES部门去北京旅游,RESEARCH部门去上海旅游,ACCOUNTING部门去桂林旅游
 8 SELECT e.ename,d.dname,(CASE WHEN d.dname="SALES" THEN "BEIJING" WHEN d.dname="RESEARCH" THEN "SHANGHAI" ELSE "GUILIN" END) AS loc FROM t_emp e JOIN t_dept d ON e.deptno=d.deptno ORDER BY d.dname;
 9 # 贼多贼多条件的数据更新
10 UPDATE t_emp e LEFT JOIN t_dept d ON e.deptno=d.deptno JOIN (SELECT deptno,AVG(sal) avg FROM t_emp GROUP BY deptno) t ON e.deptno=d.deptno SET e.sal=
11 (CASE
12 WHEN d.dname="SALES" AND DATEDIFF(NOW(),e.hiredate)/365>=20 THEN e.sal*(1+0.1)
13 WHEN d.dname="SALES" AND DATEDIFF(NOW(),e.hiredate)/365<20 THEN e.sal*(1+0.05)
14 WHEN d.dname="ACCOUNTING" THEN e.sal+300
15 WHEN d.dname="RESEARCH" AND e.sal<t.avg THEN e.sal+200
16 WHEN e.deptno IS NULL THEN e.sal+100
17 ELSE NULL
18 END)

数字函数,日期函数,字符函数,条件函数

SQL语句之函数

标签:sha   shanghai   hang   中秋   account   select   条件   search   语句   

原文地址:https://www.cnblogs.com/fly10086/p/12523505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!