MySql基础查询-流程控制函数

#流程控制函数
#if函数
SELECT
    last_name,
    commission_pct,
IF
    ( commission_pct IS NULL, ‘没奖金,呵呵‘, ‘有奖金,嘻嘻‘ ) 
FROM
    employees

#case函数
#查询员工工资,要求如果部门号=30,工资为1.1倍,如果部门号=40,工资为1.2倍,其他部门号工资为原工资

SELECT salary AS ‘原工资‘,department_id,
CASE department_id
    WHEN 30 THEN
        salary*1.1
    WHEN 40 THEN
      salary*1.2
    ELSE
        salary
END AS ‘新工资‘ FROM employees

#job grade
#AD_PRES A
#ST_MAN B
#IT_PROG C
#SA_REP D
#ST_CLERK E

SELECT last_name,job_id,
CASE job_id
WHEN ‘AD_PRES‘  THEN ‘A‘
WHEN ‘ST_MAN‘  THEN ‘B‘
WHEN ‘IT_PROG‘  THEN ‘C‘
WHEN ‘SA_REP‘  THEN ‘D‘
WHEN ‘ST_CLERK‘  THEN ‘E‘
END Grade
FROM employees;

#1. 显示系统时间(注:日期+时间)
SELECT NOW()
#2. 查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new salary)
SELECT employee_id,last_name,salary AS ‘原工资‘,salary*1.2 AS ‘新工资‘ FROM employees
#3. 将员工的姓名按首字母排序,并写出姓名的长度(length)
SELECT SUBSTR(last_name,1,1) AS ‘首字母‘,LENGTH(last_name) FROM employees ORDER BY 首字母 ASC
#4. 做一个查询,产生下面的结果
/
<last_name> earns <salary> monthly but wants <salary
3>
Dream Salary
King earns 24000 monthly but wants 72000

*/

SELECT CONCAT(last_name,‘ earns ‘,salary,‘ monthly but wants ‘,salary*3)  "Dream Salary"
FROM employees;

MySql基础查询-流程控制函数

上一篇:mysql 查询本年本季度的数据


下一篇:统计Exchange 2007收发信件数量操作方法