--自聯結 :自己和自己進行連線 如乙個表進行自己對自己聯結。
select * from products
select prod_id+vend_id from products;
select rtrim(prod_id) + rtrim(vend_id) from products;
select rtrim(prod_id) + '('+ ltrim(vend_id)+')' from products; --rtrim 去除右側多餘空白 ltrim 去除左側多餘空白
select rtrim(prod_id) + '('+ rtrim(vend_id)+')' from products;
select cust_id,cust_name,cust_contact from customers where cust_contact='jim jones'
select cust_id,cust_name,cust_contact from customers where cust_name=(select cust_name from customers where cust_contact='jim jones')
select * from customers;
--自聯結 :自己和自己進行連線 如乙個表進行自己對自己聯結。
select c1.cust_id,c1.cust_name,c1.cust_contact from customers c1,customers c2 where c1.cust_name=c2.cust_name and c2.cust_contact='jim jones'
select * from customers c1,customers c2 where c1.cust_name=c2.cust_name and c2.cust_contact='jim jones'
select * from customers c1,customers c2 where c1.cust_id=c2.cust_id and c2.cust_contact='jim jones'
--自然聯結:自然的檢索出資料,沒有重複的列。 一般用於 對第乙個表使用 (select *) 其他表明確列出子集。
select c.*,o.order_num,o.order_date,oi.item_price,oi.quantity,oi.prod_id from customers as c,orderitems as oi,orders as o where c.cust_id=o.cust_id and o.order_num=oi.order_num and oi.prod_id='rgan01'
select cust_name ,(select count(*) from orders where orders.cust_id=customers.cust_id) from customers
select cust_name ,(select count(*) from orders where orders.cust_id=customers.cust_id) from customers group by cust_name ,cust_id;
select cust_name from customers where cust_id in(select orders.cust_id from orders)
select orders.order_num ,(select count(*) from customers where customers.cust_id=orders.cust_id) from orders
select * from products;
select * from orderitems;
--select p.*,(select quantity from orderitems where orderitems.prod_id = p.prod_id) from products p
select o.quantity ,(select count(*) from products where products.prod_id=o.prod_id) from orderitems o
select o.prod_id, o.quantity , p.prod_name,p.prod_price from orderitems o ,products p where p.prod_id=o.prod_id
select o.prod_id, o.quantity , p.prod_name,p.prod_price from orderitems o ,products p
select customers.cust_name,orders.order_num from customers inner join orders on customers.cust_id=orders.cust_id;
select cust_name ,(select count(*) from orders where orders.cust_id=customers.cust_id) from customers
--外聯結 和內聯結 外聯結: 檢索出的資料還包括沒有關聯行的行。 內聯結:只檢索關聯行的資料。
--可以理解為 聯結時 聯結的行不成立也可以檢索出來。 比如null。
--如果是用的左外聯結 以左表為主, 右外聯結 以右表為主。
--全外聯結: full outer join on full outer join on
--左聯結和右聯結可以互換使用。 可以調整表名的順序來變化使用。 a表和b表, 左聯結 a,b 右聯結 b,a
-- 語法 左外聯結: left outer join on left outer join on 右外聯結: right outer join on right outer on
--外聯結
select cust_name,order_num from customers left outer join orders on customers.cust_id=orders.cust_id; --左外聯結
select cust_name ,order_num from orders left outer join customers on orders.cust_id=customers.cust_id;
select cust_name ,order_num from customers right outer join orders on customers.cust_id=orders.cust_id; --右外聯結
select cust_name ,order_num from customers full outer join orders on customers.cust_id=orders.cust_id; --全外聯結
select cust_name ,order_num from orders full outer join customers on orders.cust_id=customers.cust_id; --全外聯結
select * from orders;
--使用帶聚集函式的聯結
select customers.cust_id,count(orders.order_num) from customers inner join orders on customers.cust_id=orders.cust_id group by customers.cust_id
select * from customers;
SQL SERVER 入門高階教程 高階聯結表
高階聯結表 迄今為止我們所使用的只是內聯結的簡單聯結,但sql除了內聯結還有外聯結 outer join 當然也有人叫左聯結 右聯結以及全聯結,其實都是乙個意思,只要理解了內聯結和外聯結,左右聯結就不難了,這也使最常用的高階聯結了。注意 在學習高階聯結前,我們先來學習乙個特殊功能表別名,故名思意就是...
SQL SERVER 入門高階教程 聯結表
聯結表 其實sql最強大的功能之一就是能在資料查詢的執行重使用聯結表。這裡還要理解一下關聯式資料庫,關聯式資料庫就是設計時將資訊分解成多個表,一類資料乙個表,各個表通過某些共同的值相互關聯。舉個栗子 比如要設計乙個資料庫用來儲存 商資訊和 商產品資訊,如果要是使用乙個表是可以是實現的就是會出現很多重...
SQL使用子查詢 聯結表 建立高階聯結
利用子查詢進行過濾select cust name,cust contact from customers where cust id in select cust id from orders where order num in select order num from orderitems ...