ํ๋ก๊ทธ๋๋ฐ ์ธ๊ณ๋ฅผ ํ๊ตฌํฉ์๋ค.
<์กฐ์ธ(join) ๊ตฌ๋ถ ์์ฑํ๋ ๋ฐฉ๋ฒ>
- Inner Join ๋ฐฉ์์ : ์ผ๋ฐ๋ฐฉ์ or ํ์ค๋ฐฉ์ ๋ชจ๋ ํ์ค์ฒ๋ผ ์ฌ์ฉ๋จ
(Inner Join - ์ผ๋ฐ๋ฐฉ์)
Select ํ
์ด๋ธ1.์ปฌ๋ผ1...ํ
์ด๋ธn.์ปฌ๋ผ1
From ํ
์ด๋ธ1, ํ
์ด๋ธ2 .... ํ
์ด๋ธ n
Where ๊ด๊ณ์กฐ๊ฑด(PK = FK)
And ์ผ๋ฐ์กฐ๊ฑด
(Inner Join - ํ์ค๋ฐฉ์)
Select ํ
์ด๋ธ1.์ปฌ๋ผ1...ํ
์ด๋ธn.์ปฌ๋ผ1
From ํ
์ด๋ธ1 Inner Join ํ
์ด๋ธ2
On(๊ด๊ณ์กฐ๊ฑด(PK = FK)
And ์ผ๋ฐ์กฐ๊ฑด)
Inner Join ํ
์ด๋ธ n
On(๊ด๊ณ์กฐ๊ฑด(PK = FK)
And ์ผ๋ฐ์กฐ๊ฑด)
Group By - ๊ทธ๋ฃน์ด ์๋ ๊ฒฝ์ฐ
Having - ๊ทธ๋ฃน ์กฐ๊ฑด์ด ์๋ ๊ฒฝ์ฐ
Order By - ์ ๋ ฌ์ด ์๋ ๊ฒฝ์ฐ
1. Join์ ์ข ๋ฅ
ํฌ๋ก์ค ์กฐ์ธ(Cross Join), ์ด๋์กฐ์ธ(Inner Join), ์์ฐํฐ ์กฐ์ธ(Outer Join)
[ํฌ๋ก์ค ์กฐ์ธ (Cross Join)]
-- :์๋์ ๊ฐ์ด ๋ ํ
์ด๋ธ๊ฐ์ ๊ด๊ณ ์กฐ๊ฑด ์์ด ์ฌ์ฉํ ๊ฒฝ์ฐ
-- : ํ์ ๊ฐฏ์ = ํ
์ด๋ธํ ์ ์ฒด๊ฐฏ์ * ํ
์ด๋ธํ ์ ์ฒด๊ฐฏ์
[ ํ์ฉ ์์ ]
SELECT mem_id, cart_member
FROM member, cart, prod;
[ ๊ฒฐ๊ณผ ]
์ ๊ฒฐ๊ณผ๋ ์๋ ๊ณ์ฐ๊ณผ ๊ฐ์ต๋๋ค.
member์ cart์ ์๋ฅผ ๋ชจ๋ ๊ณ์ฐํ ๊ฒฐ๊ณผ 28๊ณผ 135๋ฅผ ๊ณฑํ ๊ฐ 279,720์ ๋๋ค.
-- 28
SELECT COUNT(*) FROM member;
-- 135
SELECT COUNT(*) FROM cart;
SELECT 28 * 135;
[ ์ด๋ ์กฐ์ธ (Inner Join) ]
-- : ๋ ํ
์ด๋ธ๊ฐ์ PK = FK ์ธ ์กฐ๊ฑด์ ์ ์
-- (์ผ๋ฐ๋ฐฉ์)
-- : From์ ๋ค์ ํ
์ด๋ธ๋ค์ ์ฝค๋ง(,)๋ก ๊ตฌ๋ถํ์ฌ ์ฌ์ฉ
-- : Where์ ์ ๊ด๊ณ์กฐ๊ฑด์(PK = FK)์ ์ถ๊ฐํจ
-- : ๊ด๊ณ์กฐ๊ฑด์์ ์ต์ํ (์ฌ์ฉ๋ ํ
์ด๋ธ์ ๊ฐฏ์ - 1)๋งํผ ์ ์๋์ด์ผ ํจ
[ ํ์ฉ ์์ (์ผ๋ฐ ๋ฐฉ์) ]
-- (์ผ๋ฐ๋ฐฉ์)
-- : From์ ๋ค์ ํ
์ด๋ธ๋ค์ ์ฝค๋ง(,)๋ก ๊ตฌ๋ถํ์ฌ ์ฌ์ฉ
-- : Where์ ์ ๊ด๊ณ์กฐ๊ฑด์(PK = FK)์ ์ถ๊ฐํจ
-- : ๊ด๊ณ์กฐ๊ฑด์์ ์ต์ํ (์ฌ์ฉ๋ ํ
์ด๋ธ์ ๊ฐฏ์ - 1)๋งํผ ์ ์๋์ด์ผ ํจ
SELECT mem_id, mem_name, cart_prod, cart_qty, prod_name
FROM member, cart, prod
-- ๊ด๊ณ์กฐ๊ฑด ์ถ๊ฐ(PK = FK)
WHERE mem_id = cart_member
AND cart_prod = prod_id
-- ์ผ๋ฐ์กฐ๊ฑด ์ถ๊ฐ
AND mem_add1 LIKE '์์ธ%';
[ ํ์ฉ ์์ (Ansi ํ์ค๋ฐฉ์) ]
-- (inner join Ansi ํ์ค๋ฐฉ์)
SELECT mem_id, mem_name, cart_prod, cart_qty, prod_name
FROM member
INNER JOIN cart
ON(mem_id = cart_member
-- ์ผ๋ฐ์กฐ๊ฑด ์ถ๊ฐ
AND mem_add1 LIKE '์์ธ%')
Inner JOIN prod
ON(cart_prod = prod_id);
[ ํ์ฉ ์์ (Ansi ํ์ค๋ฐฉ์) where ์ ์ฌ์ฉ ]
-- ์ผ๋ฐ์กฐ๊ฑด์ where ์ ์ ์ถ๊ฐ
SELECT mem_id, mem_name, cart_prod, cart_qty, prod_name
FROM member
INNER JOIN cart
ON(mem_id = cart_member)
Inner JOIN prod
ON(cart_prod = prod_id)
-- ์ผ๋ฐ์กฐ๊ฑด ์ถ๊ฐ
where mem_add1 LIKE '์์ธ%';
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ์ฃผ๋ฌธ ๋ด์ญ์ด ์๋ ์ํ์ ๋ํ ์ ๋ณด๋ฅผ ์กฐํํ๋ ค๊ณ ํฉ๋๋ค.
-- ์กฐํ์ปฌ๋ผ : ์ํ์ฝ๋, ์ํ๋ช
, ์ํ๋ถ๋ฅ๋ช
, ๊ฑฐ๋์ฒ๋ช
-- ๋จ, ์ฃผ๋ฌธ์๋์ด 5๊ฐ ์ด์ ์ฃผ๋ฌธํ ๊ฒฝ์ฐ
-- ์ฃผ๋ฌธ ํ์์ ์ง์ญ์ด ์์ธ, ๋์ , ๊ด์ฃผ์ธ ๊ฒฝ์ฐ์ ๋ํด์
[ ์์ฑ ์ฝ๋ 1 - select ํ์ฉ]
SELECT Prod_id, Prod_name,
(select Lprod_nm FROM lprod WHERE lprod_gu=prod_lgu) AS lprod_nm,
(select Buyer_name FROM buyer WHERE Buyer_id=prod_buyer) AS buyer_name
FROM prod
WHERE
prod_id IN (SELECT cart_prod FROM cart WHERE cart_qty >= 5
AND cart_member IN
(SELECT mem_id FROM member WHERE SUBSTRING(mem_add1, 1, 2) IN ('์์ธ', '๋์ ', '๊ด์ฃผ')));
[ ์์ฑ ์ฝ๋ 2 - join ํ์ฉ]
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ์กฐํ์ปฌ๋ผ : ํ์์์ด๋, ํ์์ด๋ฆ, ์ฃผ๋ฌธ๋ฒํธ, ์ฃผ๋ฌธ์๋, ์ํ๋ช
์กฐํ
-- ์กฐ๊ฑด : ์ฃผ๋ฌธ๋ด์ญ์ด ์๋ ํ์์ด์ด์ผ ํจ
-- : ์ฃผ๋ฌธํ ํ์ ๊ฑฐ์ฃผ์ง๊ฐ ์์ธ์ ๊ฑฐ์ฃผํ๋ ํ์์ด์ด์ผ ํจ
-- : ์ํ๋ช
์ ์ปดํจํฐ๋ผ๋ ๋จ์ด๊ฐ ํฌํจ๋์ด ์์ด์ผ ํจ
-- ์ ๋ ฌ : ์ฃผ๋ฌธ์๋์ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์
[ ์์ฑ ์ฝ๋ 1 - inner join ์ผ๋ฐ ๋ฐฉ์ ]
-- ์ผ๋ฐ๋ฐฉ์
SELECT Mem_id, Mem_name, Cart_no, Cart_qty, Prod_name
FROM member, cart, prod
-- ๊ด๊ณ์กฐ๊ฑด
WHERE mem_id = cart_member
AND cart_prod = prod_id
-- ์ผ๋ฐ์กฐ๊ฑด
AND mem_add1 LIKE '%์์ธ%'
AND prod_name LIKE '%์ปดํจํฐ%'
ORDER BY Cart_qty desc;
[ ์์ฑ ์ฝ๋ 2 - inner join Ansi ํ์ค ๋ฐฉ์ ]
-- Ansi ํ์ค ๋ฐฉ์
SELECT Mem_id, Mem_name, Cart_no, Cart_qty, Prod_name
FROM member
INNER JOIN cart
ON (mem_id = cart_member
AND mem_add1 LIKE '%์์ธ%')
INNER JOIN prod
ON (cart_prod = prod_id
AND prod_name LIKE '%์ปดํจํฐ%')
ORDER BY Cart_qty desc;
[ ์์ฑ ์ฝ๋ 3 - inner join Ansi ํ์ค ๋ฐฉ์ (where์ ์ฌ์ฉ)]
-- Ansi ํ์ค ๋ฐฉ์
SELECT Mem_id, Mem_name, Cart_no, Cart_qty, Prod_name
-- ๊ด๊ณ์กฐ๊ฑด
FROM member
INNER JOIN cart
ON (mem_id = cart_member)
INNER JOIN prod
ON (cart_prod = prod_id)
-- ์ผ๋ฐ์กฐ๊ฑด
WHERE mem_add1 LIKE '%์์ธ%'
AND prod_name LIKE '%์ปดํจํฐ%'
ORDER BY Cart_qty desc;
[ ๊ฒฐ๊ณผ - ๊ฒฐ๊ณผ ์์ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ์ํ๋ถ๋ฅ๋ณ ์ํ์ ๊ฐฏ์๋ฅผ ์กฐํํ๋ ค๊ณ ํฉ๋๋ค.
-- ์กฐํ์ปฌ๋ผ : ์ํ๋ถ๋ฅ์ฝ๋, ์ํ๋ถ๋ฅ๋ช
, ๋ถ๋ฅ๋ณ ์ํ์ ๊ฐฏ์
-- ์ ๋ ฌ : ๋ถ๋ฅ๋ณ๊ฐฏ์ ๋ด๋ฆผ์ฐจ์
[ ์์ฑ ์ฝ๋ 1 - inner join ์ผ๋ฐ ๋ฐฉ์ ]
-- ์ผ๋ฐ๋ฐฉ์
SELECT lprod_gu, lprod_nm, COUNT(prod_id) AS prod_cnt
FROM lprod, prod
WHERE lprod_gu = prod_lgu
GROUP BY lprod_gu, lprod_nm
ORDER BY prod_cnt;
[ ์์ฑ ์ฝ๋ 2 - inner join Ansi ํ์ค ๋ฐฉ์ ]
-- ansi ํ์ค ๋ฐฉ์
SELECT lprod_gu, lprod_nm, COUNT(prod_id) AS prod_cnt
FROM lprod
INNER JOIN prod
ON (lprod_gu = prod_lgu)
GROUP BY lprod_gu, lprod_nm
ORDER BY prod_cnt;
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
- [๋ฌธ์ ]
/*
- ํ์์์ด๋, ํ์์ด๋ฆ, ์ฃผ๋ฌธ์๋, ์ํ๋ช
์กฐํ
- ๋จ, ๊ตฌ๋งค์ํ์ ๊ฑฐ๋์ฒ ์ฃผ์๊ฐ ์์ธ, ๋์ , ๊ด์ฃผ์ธ ๊ฒฝ์ฐ
- ์ํ๋ถ๋ฅ๋ช
์ '์ ์'๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ
- ์ฃผ๋ฌธ์๋์ด 5์ด์์ธ ๊ฒฝ์ฐ
- ์ ๋ ฌ์ ์์ด๋ ์ค๋ฆ์ฐจ์, ์ฃผ๋ฌธ์๋ ๋ด๋ฆผ์ฐจ์
*/
[ ์์ฑ ์ฝ๋ 1 - inner join ์ผ๋ฐ ๋ฐฉ์ ]
-- ์ผ๋ฐ๋ฐฉ์
SELECT Mem_id, Mem_name, Cart_qty, Prod_name
FROM member, cart, prod, buyer, lprod
WHERE mem_id = cart_member
AND cart_prod = prod_id
AND prod_buyer = buyer_id
AND prod_lgu = lprod_gu
AND SUBSTRING(Buyer_add1, 1, 2) IN ('์์ธ', '๋์ ', '๊ด์ฃผ')
AND Lprod_nm LIKE '%์ ์%'
AND Cart_qty >= 5
ORDER BY mem_id ASC, Cart_qty desc;
[ ์์ฑ ์ฝ๋ 2 - inner join Ansi ํ์ค ๋ฐฉ์ ]
-- Ansi ํ์ค ๋ฐฉ์
SELECT Mem_id, Mem_name, Cart_qty, Prod_name
FROM member
inner join cart
ON (mem_id = cart_member)
inner join prod
ON (cart_prod = prod_id
AND Cart_qty >= 5)
inner join buyer
ON (prod_buyer = buyer_id
AND SUBSTRING(Buyer_add1, 1, 2) IN ('์์ธ', '๋์ ', '๊ด์ฃผ'))
inner join lprod
ON (prod_lgu = lprod_gu
AND Lprod_nm LIKE '%์ ์%')
ORDER BY mem_id ASC, Cart_qty desc;
[ ๊ฒฐ๊ณผ ] - ์ถ๋ ฅ ๊ฒฐ๊ณผ ์์
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ์ฃผ๋ฌธ๋ด์ญ์ด ์๋ ํ์๋ณ๋ก ์ง๊ธ๊น์ง ์ด ์ง์ถํ ์ด์ก ์กฐํํ๊ธฐ
-- ์กฐํ์ปฌ๋ผ : ํ์์์ด๋, ํ์์ด๋ฆ, ์ด์ง์ถ์ก
[ ์์ฑ ์ฝ๋ 1 - inner join ์ผ๋ฐ ๋ฐฉ์ ]
-- ์ผ๋ฐ๋ฐฉ์
SELECT Mem_id, Mem_name, SUM(cart_qty * prod_sale) AS total
FROM member, cart, prod
WHERE mem_id = cart_member
AND cart_prod = prod_id
GROUP BY mem_id, mem_name
ORDER BY total desc;
[ ์์ฑ ์ฝ๋ 2 - inner join Ansi ํ์ค ๋ฐฉ์ ]
-- ํ์ค๋ฐฉ์
SELECT Mem_id, Mem_name, SUM(cart_qty * prod_sale) AS total
FROM member
INNER join cart
ON(mem_id = cart_member)
INNER join prod
ON(cart_prod = prod_id)
GROUP BY mem_id, mem_name
ORDER BY total desc;
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- ์ ๋ฌธ์ ์ ์ด์ด์
-- ์ ๊ฒฐ๊ณผ์์ ๊ฐ์ฅ ์ง์ถ์ด ํฐ ๊ฐ๊ณผ, ๊ฐ์ฅ ์์ ๊ฐ์ ์กฐํํด ์ฃผ์ธ์
-- ์กฐํ ๊ฐ : ์ต๋๊ฐ, ์ต์๊ฐ
[ from ์ ์์ ์๋ธ์ฟผ๋ฆฌ ]
๊ฐ์ํ ์ด๋ธ
-- ๊ฐ์ํ
์ด๋ธ ๊ฐ๋
์ด ์ ์ฉ๋จ(Inline view)
-- : From์ ๋ค์ ํ
์ด๋ธ ๋์ ์ Sub Query๋ฅผ ์ฌ์ฉํจ
-- : Sub Query์ Selectํ ํ๋ ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ๋ก๋ฆฌ์ ์ ์ฌ์์ผ์ ์ฌ์ฉ
-- : ํ๋ ฌ์ ๋ฐ์ดํฐ๋ฅผ ํ
์ด๋ธ๊ณผ ๋์ผํ ๊ฐ๋
์ผ๋ก ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์
-- ๊ฐ์ํ
์ด๋ธ์ด๋ผ๊ณ ์นญํฉ๋๋ค.
-- : ๊ฐ์ํ
์ด๋ธ์ ๋ณ์นญ์ ์ด์ฉํด์ ํ
์ด๋ธ ์ด๋ฆ์ฒ๋ผ ์ฌ์ฉํฉ๋๋ค.
[ ์กฐ๊ฑด ]
-- ์ ๋ฌธ์ ์ ์ด์ด์
-- ์ ๊ฒฐ๊ณผ์์ ๊ฐ์ฅ ์ง์ถ์ด ํฐ ๊ฐ๊ณผ, ๊ฐ์ฅ ์์ ๊ฐ์ ์กฐํํด ์ฃผ์ธ์
-- ์กฐํ ๊ฐ : ์ต๋๊ฐ, ์ต์๊ฐ
[ ์์ฑ ์ฝ๋ ]
SELECT MAX(a.total), MIN(total)
from(SELECT Mem_id, Mem_name, SUM(cart_qty * prod_sale) AS total
FROM member, cart, prod
WHERE mem_id = cart_member
AND cart_prod = prod_id
GROUP BY mem_id, mem_name
ORDER BY total DESC) A;
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
/*
- ํ์์์ด๋ b001 ํ์์ ๋ง์ผ๋ฆฌ์ง๊ฐ๋ณด๋ค ํฐ ํ์์ ๋ณด ์กฐํํ๊ธฐ
- ์กฐํ์ปฌ๋ผ : ํ์์์ด๋, ์ด๋ฆ
- 2๊ฐ์ง ์ด์์ ๋ฐฉ๋ฒ ์ ์ฉ
*/
[ ์์ฑ ์ฝ๋ 1 ]
SELECT Mem_id, Mem_name
FROM member
WHERE Mem_mileage > (SELECT mem_mileage
FROM member
WHERE mem_id='b001');
[ ์์ฑ ์ฝ๋ 2 ]
SELECT Mem_id, Mem_name
FROM member,
(SELECT mem_mileage
FROM member
WHERE mem_id='b001') mem
WHERE member.mem_mileage > mem.mem_mileage;
[ ์์ฑ ์ฝ๋ 3 - self join ]
/*self join์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ*/
SELECT M1.mem_id, M1.mem_name
FROM member M1, member M2
WHERE M2.mem_id = 'b001'
AND M1.mem_mileage > M2.mem_mileage;
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ํ์๋ณ ๊ตฌ๋งค๊ธ์ก์ ์ด์ก์ ์กฐํํ๋ ค๊ณ ํฉ๋๋ค.
-- 2005๋
5์์ ๊ตฌ๋งคํ ๋ด์ญ์
๋๋ค.
-- ์กฐํ์ปฌ๋ผ : ํ์์ด๋ฆ, ๊ตฌ๋งค๊ธ์ก์ด์ก
[ ์์ฑ ์ฝ๋ 1 ]
-- (์ผ๋ฐ๋ฐฉ์)
SELECT mem_name,
sum(cart_qty * prod_sale) AS total
FROM member, cart, prod
WHERE mem_id = cart_member
AND cart_prod = prod_id
AND cart_no LIKE '200505%'
GROUP BY mem_id, mem_name
ORDER BY total DESC;
[ ์์ฑ ์ฝ๋ 2 - Ansi ํ์ค ๋ฐฉ์ ]
-- (ํ์ค๋ฐฉ์)
SELECT mem_name,
sum(cart_qty * prod_sale) AS total
FROM member
inner join cart
ON (mem_id = cart_member
AND cart_no LIKE '200505%')
inner join prod
ON (cart_prod = prod_id)
WHERE mem_id = cart_member
GROUP BY mem_id, mem_name
ORDER BY total DESC;
[ ๊ฒฐ๊ณผ ]
[Outer Join]
- ํน์ ํ ์ด๋ธ ์ ์ฒด์ ๋ํ ์ง๊ณ๋ฅผ ํ๊ณ ์ ํ ๋ ์ฌ์ฉ
- Outer Join ์ข
๋ฅ
: Left Outer Join : ํ
์ด๋ธ ์์ ์ค ์ผ์ชฝ์ ์์นํ ํ
์ด๋ธ ์ ์ฒด
: Right Outer Join : ์ค๋ฅธ์ชฝ์ ์์นํ ํ
์ด๋ธ ์ ์ฒด
: Full Outer Join : ์ผ์ชฝ ์ค๋ฅธ์ชฝ ์ ์ฒด
: Oracle DB์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ
- Outer Join ๊ฐ๋
: Outer Join์ ํ์ค๋ฐฉ์์ผ๋ก๋ง ์ฌ์ฉํด์ผ ํ๋ฉฐ,
: ์ผ๋ฐ ์กฐ๊ฑด์ ๋ฌด์กฐ๊ฑด On() ๋ด๋ถ์ ์์ฑํด์ผ ํ๋ค.
: Inner Join์ ๋ง์กฑํด์ผ ํจ(Inner Join ๊ฐ๋
์ ๊ทธ๋๋ก ์ ์ฉ)
: Left or Right ๊ธฐ์ค์ผ๋ก ๊ฐ์ผ๋ฉด ๊ฐ์ ์กฐ๊ฑด๋๋ก ์กฐํ, ๋ค๋ฅด๋ฉด Null ์กฐํ
Left outer join ์์
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ํ์๋ณ ๊ตฌ๋งค๊ธ์ก์ ์ด์ก์ ์กฐํํ๋ ค๊ณ ํฉ๋๋ค.
-- 2005๋
5์์ ๊ตฌ๋งคํ ๋ด์ญ์
๋๋ค.
-- ์กฐํ์ปฌ๋ผ : ํ์์ด๋ฆ, ๊ตฌ๋งค๊ธ์ก์ด์ก
-- ํ์ ์ ์ฒด์ ๋ํด์ ์ ์กฐ๊ฑด์ผ๋ก ์กฐํํด์ฃผ์ธ์
-- ์ด ํ์์ 28๋ช
์
๋๋ค.
-- null ๊ฐ์ 0์ผ๋ก ๋ณํํ์ธ์.
[ ์์ฑ ์ฝ๋ ]
-- (ํ์ค๋ฐฉ์)
SELECT mem_name,
SUM(nvl(cart_qty * prod_sale, 0)) AS total
FROM member
left outer join cart
ON (mem_id = cart_member
AND cart_no LIKE '200505%')
left outer join prod
ON (cart_prod = prod_id)
GROUP BY mem_id, mem_name
ORDER BY total DESC;
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ์ํ๋ถ๋ฅ ์ ์ฒด์ ๋ํ ์ํ ์ข
๋ฅ์ ๊ฐฏ์ ์ง๊ณํ๊ธฐ
[ ์์ฑ ์ฝ๋ ]
SELECT lprod_gu,COUNT(prod_id) AS cnt
FROM lprod
LEFT OUTER JOIN prod
ON (lprod_gu = prod_lgu)
GROUP BY lprod_gu;
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
/*
2005๋
๋ ์๋ณ ํ๋งค ํํฉ ๊ฒ์ํ๊ธฐ
์กฐํ ์ปฌ๋ผ : ํ๋งค์, ์๋ณ ์ดํ๋งค์๋, ์๋ณ ์ดํ๋งค๊ธ์ก
*/
[ ์์ฑ ์ฝ๋ ]
SELECT SUBSTRING(cart_no, 5, 2) AS MONTH,
sum(cart_qty) AS total_qty,
sum(cart_qty * prod_sale) AS total
FROM cart
left OUTER join prod
ON(cart_prod = prod_id
AND SUBSTRING(cart_no, 1, 4) = '2005')
GROUP BY MONTH;
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- 2005๋
๋์ ๋ํ ์ ์ฒด ๊ฑฐ๋์ฒ๋ณ ์ด๋งค์ถ๊ธ์ก ์กฐํํ๊ธฐ
-- ์กฐํ ์ปฌ๋ผ : ๊ฑฐ๋์ฒ์ฝ๋, ๊ฑฐ๋์ฒ๋ช
, ์ด๋งค์ถ๊ธ์ก
-- ์ ๋ ฌ : ์ด๋งค์ถ๊ธ์ก ์ค๋ฆ์ฐจ์
[ ์์ฑ ์ฝ๋ ]
SELECT buyer_id, buyer_name,
SUM(nvl(cart_qty * prod_sale,0)) AS total
FROM buyer
left outer JOIN prod
ON(buyer_id = prod_buyer)
left outer JOIN cart
ON(prod_id = cart_prod
AND cart_no LIKE '2005%')
GROUP BY buyer_id, buyer_name
ORDER BY total desc;
[ ๊ฒฐ๊ณผ ]
[ Union ]
-- : ์กฐํ๊ฒฐ๊ณผ์ ์กฐํ๊ฒฐ๊ณผ๋ฅผ ํ๋จ์๋ก ํฉ์น๋ ๊ธฐ๋ฅ
-- : ์กฐํ๊ฒฐ๊ณผ๋ค๊ฐ์ ์ปฌ๋ผ์ ๊ฐฏ์๋ ๋์ผํด์ผ ํจ
-- : ์กฐํ๊ฒฐ๊ณผ๋ค๊ฐ์ ๋ฐ์ดํฐ ํ์
์ ๋์ผํด์ผ ํจ
-- Union์ ์ฌ์ฉํ๊ฒ ๋๋ฉด
-- : ์ ๋ ฌ์ ์ ์ผ ๋ง์ง๋ง์ ์ ์ํด์ผ ํจ
-- : ์ปฌ๋ผ๋ช
์ ์ฒซ๋ฒ์ฌ select๋ฌธ์ ์ปฌ๋ผ์ด๋ฆ์ ๋ฐ๋ฆ
-- Union : ์ค๋ณต ์ ๊ฑฐ
-- Union All : ์ค๋ณต ํฌํจ(์ค์ ์ ์ฒด)
[ ํ์ฉ ์์ ]
SELECT 'member', mem_id, mem_name
FROM member
UNION
SELECT 'cart',cart_member, cart_prod
FROM cart;
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ํ์์ ์ฒด์ ๋ํ ์ด๊ตฌ๋งค๊ธ์ก์ ์กฐํํ๊ธฐ
-- ์กฐํ์ปฌ๋ผ : ํ์์์ด๋, ํ์์ด๋ฆ, ์ด๊ตฌ๋งค๊ธ์ก
-- ๋ง์ง๋ง ํ์๋ ์ด๊ตฌ๋งค๊ธ์ก์ ์ดํฉ์ ํ์ํ๊ณ ์ถ์ด์
[ ์์ฑ ์ฝ๋ ]
SELECT mem_id, mem_name,
SUM(nvl(cart_qty * prod_sale, 0)) AS total
FROM member
LEFT OUTER JOIN cart
ON(mem_id = cart_member)
LEFT OUTER JOIN prod
ON(cart_prod = prod_id)
GROUP BY mem_id, mem_name
UNION
SELECT '', '', SUM(nvl(cart_qty * prod_sale, 0)) AS total
FROM member
LEFT OUTER JOIN cart
ON(mem_id = cart_member)
LEFT OUTER JOIN prod
ON(cart_prod = prod_id)
GROUP BY '', '';
[ ๊ฒฐ๊ณผ ]
-- exists()
-- : ์กฐํ๊ฒฐ๊ณผ๊ฐ 1๊ฑด์ด๋ผ๋ ์์ผ๋ฉด True
-- : 0๊ฑด์ด๋ฉด False
-- : ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์ ์ฉ (๋ค์ค์ปฌ๋ผ์ ๋ค์คํ ๋ชจ๋ ๊ฐ๋ฅ)
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ๊ตฌ๋งค(์ฃผ๋ฌธ) ๋ด์ญ์ด ์๋ ํ์๋ง ์กฐํํด์ฃผ์ธ์.
-- exists ์ฌ์ฉ
[ ์์ฑ ์ฝ๋ 1 ]
SELECT mem_id, mem_name
FROM member
WHERE EXISTS(SELECT *
FROM cart
WHERE cart_member = mem_id);
[ ๊ฒฐ๊ณผ ]
[ ์์ฑ ์ฝ๋ 2 ]
-- [๋ฌธ์ ]
-- ๊ตฌ๋งค(์ฃผ๋ฌธ) ๋ด์ญ์ด ์๋ ํ์๋ง ์กฐํํด์ฃผ์ธ์.
-- exists ์ฌ์ฉ
SELECT Mem_id, mem_name
FROM member
WHERE not exists(SELECT *
from cart
where mem_id=cart_member);
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [ ๋ฌธ์ ]
-- 2005๋
๋ ๊ตฌ๋งค๋ด์ญ์ด ์๋ ํ์์ ๋ํ
-- ํ์์์ด๋, ํ์์ด๋ฆ, ๋ง์ผ๋ฆฌ์ง ์กฐํ
-- ๋จ, ๊ตฌ๋งค๋ด์ญ์ด ์๋ ํ์์ ์ด๊ตฌ๋งค๊ธ์ก์ด
-- 3์ฒ๋ง์ ์ด์์ธ ๋ฐ์ดํฐ์ ๋ํด์๋ง ์กฐํ
[ ์์ฑ ์ฝ๋ ]
SELECT mem_id, mem_name, mem_mileage
FROM member
WHERE EXISTS(
SELECT SUM(cart_qty * prod_sale)
FROM cart, prod
WHERE cart_member = mem_id
AND cart_prod = prod_id
AND cart_no LIKE '2005%'
HAVING SUM(cart_qty * prod_sale) >= 30000000
);
[ ๊ฒฐ๊ณผ ]
[ Update ]
-- ์์ ๋ฐ ์ญ์ ์
-- ๊ธฐ์ค ์ปฌ๋ผ์ ๋๊ตฌ?
-- PK๊ฐ์ ๊ธฐ์ค์ผ๋ก ํ์ ์กฐ๊ฑด์ฒ๋ฆฌ ํด์ผ ํฉ๋๋ค.
[ ์กฐ๊ฑด ]
-- ์์ ํ๊ธฐ
-- ํ์์์ด๋ a001, b001์ธ ํ์์
-- ๋ง์ผ๋ฆฌ์ง ๊ฐ์ 2๋ก ์์ ํด์ฃผ์ธ์
-- (๊ฒ์ฆ์ฉ select ๋ฌธ)
[ ์์ฑ ์ฝ๋ ]
-- (๊ฒ์ฆ์ฉ select ๋ฌธ)
SELECT mem_id, mem_mileage
FROM member
WHERE mem_id IN ('a001', 'b001');
--์์ ํ๊ธฐ
update member
SET mem_mileage = 2
WHERE mem_id IN ('a001', 'b001');
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ๊ตฌ๋งค๋ด์ญ์ด ์๋ ํ์์ ๋ง์ผ๋ฆฌ์ง ๊ฐ์
-- 3์ผ๋ก ์ผ๊ด ์์ ์ฒ๋ฆฌ ํด ์ฃผ์ธ์.
[ ์์ฑ ์ฝ๋ ]
-- (๊ฒ์ฆ์ฉ select ๋ฌธ)
SELECT mem_id, mem_mileage
FROM member
WHERE EXISTS
(SELECT cart_member FROM cart WHERE cart_member = mem_id);
UPDATE member
SET mem_mileage = 3
WHERE EXISTS
(SELECT cart_member FROM cart WHERE cart_member = mem_id);
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ์ํ๋ถ๋ฅ ์ ์ฒด์ ๋ํ ์ํ์ด๋ฆ๊ณผ ๊ตฌ๋งค์๋์ ์ดํฉ์ ์กฐํ
-- ๋จ, ๊ตฌ๋งค๋
๋๊ฐ 2005๋
๋
-- prod, cart ํ
์ด๋ธ๋ง ์ฌ์ฉ
-- ์กฐํ์ปฌ๋ผ : ์ ์ฒด์ํ๋ถ๋ฅ์ฝ๋, ์ํ๋ช
, ๊ตฌ๋งค์๋์ ์ดํฉ
[ ์์ฑ ์ฝ๋ ]
SELECT LEFT(prod_id, 4) AS lgu,
prod_name,
SUM(cart_qty) AS total
FROM prod
LEFT OUTER JOIN cart
ON(prod_id = cart_prod
AND cart_no LIKE '2005%')
GROUP BY LEFT(prod_id, 4), prod_name;
[ ๊ฒฐ๊ณผ ]