1. 程式人生 > 其它 >Linux作業系統啟動詳解

Linux作業系統啟動詳解

子查詢(巢狀查詢)

1. 理解
   一個查詢語句中內嵌另一個完整的查詢語句,被巢狀的語句 稱為子查詢 或者內查詢
   外面的語句稱為主查詢 或者外查詢
2. 語法
   select (子查詢)
   from (子查詢)
   where (子查詢)

3. 特點
   1. 子查詢 必須寫在小括號中
   2. 子查詢 優先執行  ,主查詢需要用到子查詢的結果
   3. 子查詢結果
	單行子查詢:結果只有一個 搭配的符號 =  > < >= <= <>
	多行子查詢:結果多個     搭配的符號 in / not in / any(任一) / all (所有)

#案例1:查詢薪資比'De Haan'的薪資高的員工資訊
#step1: De Haan 的薪資
	select * from employees where last_name = 'De Haan';
#step2: 找到薪資比DeHaan薪資還要高的員工資訊
	#select * from employees where salary > 17000;
	select * from employees where salary > 
      (select salary from employees where last_name = 'De Haan');


#案例2:查詢location_id 是1400 或者 1700的部門中所有的員工的資訊

select * from employees where department_id in 
(select department_id from departments where location_id = 1400 or location_id = 1700);


#案例3:查詢其他工種中比工種為'IT_PROG'中的 所有員工薪資都低的員工薪資、名字
select * from employees where salary <
	(select min(salary) from employees where job_id = 'it_prog')

select * from employees where salary <
	(select salary from employees where job_id = 'it_prog' order by salary limit 1 )

select * from employees where salary < all
	(select salary from employees where job_id = 'it_prog')
		

查詢的規則:
	1. 單表能實現 不用多表
 	2. 多表能實現 不用子查詢
 	
	
子查詢效率比較低。