LessonAim
While retrieving data from the database, you may need to restrict the rows of data that are displayed or specify the order in which the rows are displayed.
This lesson explains the SQL statements that youuse to perform these actions.
?在查詢中過濾行。
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];
?WHERE 子句緊隨 FROM子句。
SELECT employee_id,last_name,job_id,department_id
FROM employees
WHERE department_id = 90 ;
?字符和日期要包含在單引號中。 ?字符大小寫敏感,日期格式敏感。
SELECTlast_name,job_id,department_id
FROM employees
WHERE last_name = 'Whalen';
SELECTlast_name,hire_date,department_id
FROM employees
WHERE hire_date ='7-6月-1994'
日期以特定的格式書寫!
SELECT last_name, salary
FROM employees
WHERE salary <= 3000;
1.
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500;
2.
SELECT employee_id,last_name, salary,manager_id
FROM employees
WHERE manager_id IN (100, 101, 201);
?使用 LIKE運算選擇類似的值 ?選擇條件可以包含字符或數字: –% 代表零個或多個字符(任意個字符)。 _代表一個字符
3.
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%';
4.
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';
?可以使用 ESCAPE 標識符選擇‘%’和‘_’符號。
?回避特殊符號的:使用轉義符。例如:將[%]轉為[\%]、[_]轉為[\_],然後再加上[ESCAPE ‘\’] 即可。
SELECT job_id
FROM jobs
WHERE job_id LIKE ‘IT\_%‘ escape ‘\‘;
5.
使用IS (NOT)NULL 判斷空值。
SELECT last_name,manager_id
FROM employees
WHERE manager_id IS NULL;