資料表單的運算 http://www.mysql.tw/2014/05/blog-post_21.html
關聯模式的運算 http://www.mysql.tw/2015/05/blog-post_14.html
資料表的運算有以下~
限制(Restrict)
投影(Project)
聯集(Union)
卡氏積(Cartesian Product)
差集(Difference)
交集(Intersection)
合併(Join)
除法(Division)
(1)限制(Restrict) : 指的是從資料表取出符合條件的資料。
從客戶表單中取出客戶編號為1的資料~
SELECT * FROM customer WHERE cus_id=1;
(2)投影(Project) : 指的是從資料表取出特定欄位。
從客戶表單中取出客戶編號與客戶姓名~
SELECT cus_id, cus_name FROM customer;
(3)聯集(Union) : 指的將兩個或多個資料表產生為新的資料表,若有重複的資料,則只顯示一次。
例如有兩個結構一樣的客戶資料表,要把他合併起來,但是要去除重複資料。
以下是測試過程~假設原本已經有customer表單
//建立一個結構跟customer一樣的資料表 customer2
create table customer2 like customer;
//要把原customer的資料也插入customer2
insert customer2 select * from customer;
然後可以在customer2多插一筆新資料,刪除一筆舊資料,以便看出聯集的作用
insert into customer2 (cus_id, cus_name, cus_address, cus_no) values (6, 'Ted', 'Chiayi', ;006');
delete from customer2 where cus_id=5;
select * from customer UNION select * from customer2;
(4)卡氏積(Cartesian Product) : 是指兩個資料表相乘。
例如~
select * from order_head, order_body;
但是如果只是單純以上指令,產生的結果是沒有意義的,必須再給其他條件。
例如~
SELECT ord_cus_id FROM order_head, order_body
WHERE order_head.ord_id=order_body.ordb_ord_id
AND order_body.ordb_prod_id=1;
(5)差集(Difference) : 是指存在其一資料表,但是不在另一資料表。
差集以符號 - 表示,例如R1-R2
例如~
select prod_id from product where prod_id NOT IN (select ordb_prod_id from order_body);
(6)交集(Intersection) : 是指存在其一資料表,也存在另一資料表。
交集(Intersection),以符號Ç,例如R1ÇR2
例如~
select prod_id from product where prod_id IN (select ordb_prod_id from order_body);
(7)合併(Join) : 將兩個關聯相乘後,再用SELECT運算子找出所需的序列值。
例如~
[INNER JOIN]
SELECT * FROM tableA a INNER JOIN tableB b ON a.key=b.key;
SELECT * FROM tableA a INNER JOIN tableB b USING (key);
SELECT * FROM tableA a, tableB b WHERE a.key=b.key;
SELECT * FROM tableA a INNER JOIN tableB b ON a.key=b.key;
SELECT * FROM tableA a INNER JOIN tableB b USING (key);
SELECT * FROM tableA a, tableB b WHERE a.key=b.key;
[LEFT OUTER JOIN/LEFT JOIN]
SELECT * FROM tableA a LEFT OUTER JOIN tableB b ON a.key=b.key WHERE [conditions];
SELECT * FROM tableA a LEFT OUTER JOIN tableB b USING (key) WHERE [conditions];
SELECT * FROM tableA a LEFT OUTER JOIN tableB b ON a.key=b.key WHERE [conditions];
SELECT * FROM tableA a LEFT OUTER JOIN tableB b USING (key) WHERE [conditions];
[RIGHT OUTER JOIN/RIGHT JOIN]
SELECT * FROM tableA a RIGHT OUTER JOIN tableB b ON a.key=b.key WHERE [conditions];
SELECT * FROM tableA a RIGHT OUTER JOIN tableB b USING (key) WHERE [conditions];
SELECT * FROM tableA a RIGHT OUTER JOIN tableB b ON a.key=b.key WHERE [conditions];
SELECT * FROM tableA a RIGHT OUTER JOIN tableB b USING (key) WHERE [conditions];
JOIN
(8)除法(Division) :
格式~
select 目標屬性 from 目標表格
where not exists
(select * from 除式表格
where not exists
(select * from 被除式表格
where 目標表格.合併屬性1=被除式表格.合併屬性1
and 除式表格.合併屬性2=被除式表格.合併屬性2));
and 除式表格.合併屬性2=被除式表格.合併屬性2));
例如~
現在要知道~哪個產品,是全部訂單都有訂購
(A) order_head(ord_id ... ) ~ A以ord_id與B的ordb_ord_id關聯
(B) order_body(ordb_id, ordb_ord_id, ordb_prod_id ...) ~ B以ordb_prod_id與P的prod_id關聯
(P) product(prod_id, prod_name ... )
A = 除式表格 (order_head)
B = 被除式表格 (order_body)
select prod_name from product as P
where not exists
(select * from order_head as A
where not exists
(select * from order_body as B
where P.prod_id=B.ordb_prod_id
and A.ord_id=B.ordb_ord_id));
現在要知道~哪個訂單,是全部產品都有訂購
P = 除式表格 (product)
B = 被除式表格 (order_body)
select ord_id from order_head as A
where not exists
(select * from product as P
where not exists
(select * from order_body as B
where A.ord_id=B.ordb_ord_id
and P.prod_id=B.ordb_prod_id));
se
更多參考
0 留言