幾種常見的MySQL查詢命令以及例子

#二、in
/* 功能:查詢某字段的值是否屬於指定的列表以內 a in(常量值1,常量值2,常量值3,...) a not in (常量值1,常量值2,常量值3,...); */
#a案例一:查詢部門編號是30/50/90的員工名和部門編號
SELECT last_name,department_id
FROM employees
WHERE department_id IN(30,50,90);

#方式二:
SELECT last_name,department_id
FROM employees
WHERE department_id =30 OR department_id=50 OR department_id=90;
#案例二:查詢工種編號不是的員工信息
SELECT *
FROM employees
WHERE job_id NOT IN ('sh_clerk','it_prog');

#3/between and/not between and
/*判斷某個字段的值是否介於什麼什麼之間 */
#案例一:部門編號是30-90之間的部門編號員工姓名
SELECT department_id,last_name
FROM employees
WHERE department_id BETWEEN 30 AND department_id<=30;

#案例二:查詢年薪不是10000-200000之間的員工姓名、工資、年薪
SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) AS 年薪
FROM employees
WHERE salary*12*(1+IFNULL(commission_pct,0))NOT BETWEEN 100000 AND 200000;

#四、is null/is not null
#案例1:查詢獎金率爲空的員工信息
SELECT *
FROM employees
WHERE commission_pct IS NOT NULL;



#= 只能判斷普通的內容
#isnull 只能判斷null值
#<=>安全等於,既能判斷普通內容,又能判斷null值
SELECT *
FROM employees
WHERE commission_pct <=> 0.3;
練習:
SELECT last_name,salary*12*(1+IFNULL(commission_pct,0)),department_id
FROM employees
WHERE employee_id=176;

SELECT last_name,salary
FROM employees
WHERE salary NOT BETWEEN 30 AND 50;

SELECT last_name,job_id
FROM employees
WHERE manager_id IS NULL;

SELECT last_name
FROM employees
WHERE last_name LIKE '__a%';

SELECT job_id 
FROM employees
WHERE department_id BETWEEN 80 AND 100;
相關文章
相關標籤/搜索