Subquery 子查詢
在一個查詢語句裡面使用另一個查詢語句的結果
(在一個查詢語句裡面塞入另一個查詢語句)
(1)用一個查詢取找`branch`中研發部門經理的`manager_id`
SELECT `manager_id` FROM `branch`
WHERE `branch_name` = '研發';
(2)再寫另一個查詢,這個查詢利用剛剛的查詢結果(`manager_id`
)去找`employee`中員工id是206的名字
SELECT `name`
FROM `employee`
WHERE `emp_id` = (
SELECT `manager_id` FROM `branch`
WHERE `branch_name` = '研發'
);
等同於這個意思 :
SELECT `name`
FROM `employee`
WHERE `emp_id` = 206;
SELECT `employee`.`name` FROM `employee`
WHERE `employee`.`emp_id` IN (
SELECT `works_with`.`emp_id` FROM `works_with`
WHERE `total_sales` > 50000
);