1. 程式人生 > >學習筆記6.15

學習筆記6.15

--給表名起別名
select cust_name,cust_contact
from customers  c,orders o,orderitems  oi
where c.cust_id=o.cust_id
and oi.order_num=o.order_num
and prod_id='RGAN01';
--oracle無as關鍵字

--自然聯結
select c.*,o.order_num,o.order_date,oi.prod_id,oi.quantity,oi.item_price
from customers c,orders o,orderitems oi
where c.cust_id=o.cust_id
and oi.order_num=o.order_num
and prod_id='RGAN01';

--內聯結
select customers.cust_id,orders.order_num
from customers inner join orders
on customers.cust_id=orders.cust_id;


--外聯結
select customers.cust_id,orders.order_num
from customers left outer join orders
on customers.cust_id=orders.cust_id;

select customers.cust_id,orders .order_num
from orders full outer join customers
on Orders.cust_id=customers.cust_id;

/*left join(左聯接) 返回包括左表中的所有記錄和右表中聯結欄位相等的記錄

right join(右聯接) 返回包括右表中的所有記錄和左表中聯結欄位相等的記錄

inner join(等值連線) 只返回兩個表中聯結欄位相等的行*/

select customers.cust_id,orders .order_num
from orders right outer join customers
on Orders.cust_id=customers.cust_id;

select customers.cust_id,count(orders.order_num) num_ord
from customers inner join orders
on customers.cust_id=orders.cust_id
group by customers.cust_id;

select customers.cust_id,
count(orders.order_num) num_ord
from customers left outer join orders
on customers.cust_id=orders.cust_id
group by customers.cust_id;