在Day5的時候,主要介紹了SELECT語句,在當中也有提及多種資料表合併的語法,這篇文會列出一些簡單的實作例題。
SELECT order_id, product_id, unit_price*quantity"Total Price"
FROM order_items
NATURAL JOIN orders
WHERE unit_price*quantity>50000;
SELECT e.employee_id, e.last_name, department_id, d.department_name
FROM employees e
JOIN departments d USING(department_id);
SELECT oi.order_id, product_id, order_date
FROM order_items oi
JOIN order o USING(order_id);
正確:
SELECT order_id, product_id, order_date
FROM order_items oi
JOIN orders o USING(order_id);
原因:輸入USING子句使用到的欄位時,欄位前不可加限定識別字。
SELECT o.order_id, o.order_date, oi.product_id, oi.unit_price, oi.quantity, oi.unit_price*oi.quantity"TOTAL"
FROM order_items oi JOIN orders o
ON(o.order_id=oi.order_id) AND o.order_date>='01-01月-2008';
SELECT e.first_name||''||e.last_name"員工", m.first_name||''||
m.last_name"主管"
FROM employees e JOIN employees m
ON(e.manager_id=m.employee_id);
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
LEFT OUTER JOIN departments d
ON e.department_id=d.department_id;