MySql basic query-process control function


#Flow Control Function#if function

SELECT
    last_name,
    commission_pct,
IF
    ( commission_pct IS NULL, '没奖金,呵呵', '有奖金,嘻嘻' ) 
FROM
    employees

#case function
# Query employee salary, if the department number=30, the salary is 1.1 times, if the department number=40, the salary is 1.2 times, and the salary of other department numbers is the original salary

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. Display system time (note: date+time)
SELECT NOW()
#2. Query employee number, name, salary, and the result of salary increase by 20% (new salary)
SELECT employee_id,last_name,salary AS '原工资',salary*1.2 AS '新工资' FROM employees
#3. Sort employee names by first letter , And write out the length of your name (length)
SELECT SUBSTR(last_name,1,1) AS '首字母',LENGTH(last_name) FROM employees ORDER BY 首字母 ASC
#4. Make a query, and produce the following result
/
<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;

Guess you like

Origin blog.51cto.com/14049943/2679330