ํ๋ก๊ทธ๋๋ฐ ์ธ๊ณ๋ฅผ ํ๊ตฌํฉ์๋ค.
ํ์ ํ ์ด๋ธ ์ ์ฒด ์กฐํํ๊ธฐ
SELECT *
FROM member;
๋ณ์นญ์ ์ฌ์ฉํ๋ ์ด์
๊ธด ๋จ์ด๋ฅผ ์ค์ฌ์ ์ฌ์ฉํ๋๋ฐ ์ข๋ค.
SQL์์ ๋ณ์นญ์ ์ฌ์ฉํ ์ ์๋ ๊ณณ
- ์ปฌ๋ผ๋ช ๋์ ๋ณ์นญ ์ฌ์ฉ
์์) mem_id as id
- ํ
์ด๋ธ๋ช
๋์ ๋ณ์นญ ์ฌ์ฉ
์์) From member mem
<์กฐ๊ฑด>
-- ํ์์ ๋ง์ผ๋ฆฌ์ง ๊ฐ์ด 10 ์ด์์ธ ๋ฐ์ดํฐ๋ง ์กฐํํ๊ธฐ
-- ์กฐํ์ปฌ๋ผ์ ํ์์์ด๋, ํ์์ด๋ฆ, ๋ง์ผ๋ฆฌ์ง
-- ๋ณ์นญ, id, name, point
-- ์ฌ์ฉํ ํ
์ด๋ธ๋ช
: member
-- ์ฌ์ฉํ ์ปฌ๋ผ๋ช
: mem_mileage, mem_id, mem_name
-- ์กฐ๊ฑด์ด ๋ฌด์์ธ์ง : mem_mileage >= 10
<์์ฑ ์ฝ๋ 1>
SELECT mem_mileage AS point, mem_id AS id, mem_name AS NAME
FROM member
WHERE mem_mileage >= 10;
<์์ฑ ์ฝ๋ 2>
SELECT mem_mileage point, mem_id id, mem_name NAME
FROM member
WHERE mem_mileage >= 10;
<์์ฑ ์ฝ๋ 3>
SELECT mem_mileage 'point', mem_id 'id', mem_name 'NAME'
FROM member
WHERE mem_mileage >= 10;
<์์ฑ ์ฝ๋ 4>
SELECT mem_mileage AS mempoint, mem_id 'id', mem_name 'NAME'
FROM member
WHERE mem_mileage >= 10;
<๊ฒฐ๊ณผ>
[ํ ์ด๋ธ์ ๋ณ์นญ ์ฌ์ฉํ๊ธฐ]
-- ํ
์ด๋ธ์ ๋ณ์นญ ์ฌ์ฉํ๊ธฐ
SELECT MEM.mem_mileage AS POINT,
mem_id AS id, mem_name AS NAME
FROM member MEM
WHERE mem_mileage >= 10;
( ๊ฒฐ๊ณผ๋ ์์ ๊ฐ์ )
[ ์ ๋ ฌ ]
์ค๋ฆ์ฐจ์ : order by ์ปฌ๋ผ๋ช ASC
๋ด๋ฆผ์ฐจ์ : order by ์ปฌ๋ผ๋ช DESC
<์กฐ๊ฑด>
ํ์์ ๋ง์ผ๋ฆฌ์ง ๊ฐ์ด 10 ์ด์์ธ ๋ฐ์ดํฐ๋ง ์กฐํํ๊ธฐ
-- ํ์์ ์ด๋ฆ์ด ๊น์๋์ธ ํ์์ ์กฐํํด ์ฃผ์ธ์.
-- ์กฐํ์ปฌ๋ผ, ํ์์์ด๋, ํ์์ด๋ฆ, ๋ง์ผ๋ฆฌ์ง
-- ์ ๋ ฌ : ํ์์์ด๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์
-- : order by ์ปฌ๋ผ๋ช
ASC(์ค๋ฆ์ฐจ์)
-- : order by ์ปฌ๋ผ๋ช
DESC(๋ด๋ฆผ์ฐจ์)
<์์ฑ ์ฝ๋>
SELECT mem_mileage, mem_id, mem_name
FROM member
WHERE mem_mileage >=10
and mem_name = '๊น์๋'
ORDER BY mem_mileage DESC, mem_id ASC;
<๊ฒฐ๊ณผ>
[ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์์ ]
1. Select
2. From
3. ํ
์ด๋ธ ๋ฉ๋ชจ๋ฆฌ ์ฌ๋ฆฌ๊ธฐ
4. Where ์กฐ๊ฑด
5. And ์กฐ๊ฑด ๋๋ OR ์กฐ๊ฑด
6. ์กฐํํ ์ปฌ๋ผ
7. ์ ๋ ฌ
[ ์ ๋ ฌ์ ์ฌ์ฉํ ์ ์๋ ์ด๋ฆ ]
์ปฌ๋ผ๋ช , ๋ณ์นญ, ์์น
SELECT mem_mileage AS POINT,
mem_id AS id, mem_name AS NAME
FROM member
WHERE mem_mileage >= 10
ORDER BY 3 DESC;
[ ์กฐ๊ฑด ]
/*
ํ์ ์ค์ 76๋
1์ 1์ผ ์ดํ์ ํ์ด๋ ํ์์ ์กฐํํด ์ฃผ์ธ์
- ๋จ, ์ฃผ๋ฏผ๋ฑ๋ก๋ฒํธ ์์๋ฆฌ 6์๋ฆฌ๋ฅผ ์ด์ฉํ๊ธฐ
- ์กฐํ์ปฌ๋ผ : ํ์์์ด๋, ์ด๋ฆ, ์ฃผ๋ฏผ๋ฒํธ์6์๋ฆฌ
*/
[ ์์ฑ ์ฝ๋ ]
SELECT mem_id, mem_name, Mem_regno1
FROM member
WHERE Mem_regno1 > '760101';
[ ๊ฒฐ๊ณผ ]
[ ์ฐ์ฐ์ ]
-- ๋น๊ต์ฐ์ฐ์ : >, <, >=, <=, =, <>, !=
-- ๋
ผ๋ฆฌ์ฐ์ฐ์ : And, Or
-- ์ฐ์ ์ฐ์ฐ์ : +, -, *, /
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ์ํ๋ถ๋ฅ์ฝ๋๊ฐ P201์ด๊ณ ,
-- ์ํํ๋งค๊ฐ๊ฒฉ์ด 17๋ง์์ธ
-- ์ํ์ฝ๋, ์ํ๋ช
, ์ํ๋ถ๋ฅ์ฝ๋, ์ํํ๋งค๊ฐ๊ฒฉ ์กฐํ
-- ์ ๋ ฌ์ ์ํ๋ช
์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์
-- ์ฌ์ฉํ ํ
์ด๋ธ : lprod, prod
-- ์ฌ์ฉํ ์ปฌ๋ผ : Prod_id, Prod_name, Prod_lgu, Prod_sale
-- ์ผ๋ฐ ์กฐ๊ฑด : prod_lgu='P201' And prod_sale=170000
-- ์ ๋ ฌ ์กฐ๊ฑด : prod_name asc
[ ์์ฑ ์ฝ๋ ]
SELECT Prod_id, Prod_name, Prod_lgu, Prod_sale
FROM prod
WHERE Prod_lgu = 'P201'
AND Prod_sale = 170000
ORDER BY Prod_name ASC;
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- ์ํ๋ช
์ ์ผ์ฑ์ด๋ผ๋ ๋จ์ด๊ฐ ์๋ ๋ฐ์ดํฐ ์กฐํํ๊ธฐ
-- ์กฐํ์ปฌ๋ผ, ์ํ๋ช
, ์ํ์ฝ๋
-- like : ๋ฌธ์๋ด ํน์ ๋จ์ด ํฌํจ์ฌ๋ถ ๊ฒ์
[ ์์ฑ ์ฝ๋ ]
SELECT Prod_name, Prod_id
FROM prod
-- ์ํ๋ช
์ค์ ์ฒซ ๊ธ์๊ฐ "์ผ"์ผ๋ก ์์ํ๋ ๋ชจ๋ ๊ฒ ์ฐพ๊ธฐ
-- WHERE Prod_name LIKE '์ผ%';
-- ์ํ๋ช
์ค์ ๋๋ฒ์งธ ๋จ์ด๊ฐ "์ฑ"์ผ๋ก ๋๋๋ ๋ชจ๋ ๊ฒ ์ฐพ๊ธฐ
-- WHERE Prod_name LIKE '_์ฑ%';
-- ์ํ๋ช
์ค์ ๋ง์ง๋ง ๊ธ์๊ฐ "์น"๋ก ๋๋๋ ๋ชจ๋ ๊ฒ ์ฐพ๊ธฐ
-- WHERE Prod_name LIKE '%์น';
-- ์ํ๋ช
์ค์ "์ฌ๋ฆ"์ด๋ผ๋ ๋จ์ด๊ฐ ํฌํจ๋์ด ์์ผ๋ฉด ์ฐพ๊ธฐ
-- WHERE prod_name LIKE '%์ฌ๋ฆ%';
WHERE prod_name LIKE '%์ผ์ฑ%';
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ํ์์ ๊ฑฐ์ฃผ ์ง์ญ์ด ์์ธ์ด๊ณ ,
-- ๋ง์ผ๋ฆฌ์ง๊ฐ 1000 ์ด์์ธ ํ์์
-- ์์ด๋, ์ด๋ฆ, ์ฃผ์(์), ๋ง์ผ๋ฆฌ์ง ์กฐํํ๊ธฐ
[ ์์ฑ ์ฝ๋ ]
SELECT Mem_id, Mem_name, concat(Mem_add1,' ' ,Mem_add2) ์ฃผ์, Mem_mileage
FROM member
WHERE Mem_add1 LIKE '%์์ธ%'
AND Mem_mileage >= 1000;
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ํ์ ์ค์ ์์ผ์ด 1975๋
๋์ ํ์ด๋ ํ์์กฐํ
-- ์กฐํ์ปฌ๋ผ์ ํ์์ด๋ฆ, ํ์์์ผ
[ ์์ฑ ์ฝ๋ ]
SELECT Mem_name, Mem_bir
FROM member
WHERE Mem_bir LIKE '1975%';
[ ๊ฒฐ๊ณผ ]
[ Between ํ์ฉํ ์ฐ์ฐ ]
-- Between A and B : A์ด์ B์ดํ์ ๋ฒ์ ์กฐ๊ฑด
-- ๋ ์ง ํ์
๋๋ ์ซ์ ํ์
์ ๋ํ ๋ฒ์ ์ฐ์ฐ ์กฐ๊ฑด์ ์ฌ์ฉ๋จ
SELECT Mem_name, Mem_bir
FROM member
-- WHERE Mem_bir BETWEEN '1975-01-01' AND '1975-12-31';
-- WHERE Mem_bir BETWEEN '19750101' AND '19751231';
-- WHERE Mem_bir BETWEEN '1975.01.01' AND '1975.12.31';
WHERE Mem_bir BETWEEN '1975/01/01' AND '1975/12/31';
[ ๋ ์ง ํ์ (ํฌ๋ฉง ํ์) ]
-- 0000-00-00, 0000.00.00, 0000/00/00, 00000000
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ํ์ ๋ง์ผ๋ฆฌ์ง์ ๊ฐ์ด 10 ์ด์์ด๊ณ , 10000 ์ดํ์ด๋ฉฐ,
-- ํ์์ ์ฑ์จ๊ฐ ๊น์จ์ธ ํ์๋ค์ ๋ํ
-- ํ์์ด๋ฆ, ๋ง์ผ๋ฆฌ์ง ์กฐํํ๊ธฐ
[ ์์ฑ ์ฝ๋ ]
SELECT Mem_name, Mem_mileage
FROM member
WHERE Mem_mileage BETWEEN 10 AND 100000
AND Mem_name LIKE '๊น%';
[ ๊ฒฐ๊ณผ ]
-- ํ์์ ์ถ์๋
๋(์์ผ๊ธฐ์ค)๊ฐ 1975๋
์์ด ์๋ ํ์์ ๋ํ
-- ํ์์ด๋ฆ, ํ์์์ผ ์กฐํ
-- Not In, Not Like, Not Between
[ ์์ฑ ์ฝ๋ ]
SELECT Mem_name, Mem_bir
FROM member
-- WHERE Mem_bir not BETWEEN '1975-01-01' AND '1975-12-31';
WHERE mem_bir NOT LIKE '1975%';
[ ๊ฒฐ๊ณผ ]
[ ์กฐ๊ฑด ]
-- ํ์์ด๋ฆ, ํ์์ฃผ๋ฏผ๋ฒํธ(์-๋ค), ์ฃผ์(์ ๋ค)
-- ์ปฌ๋ผ ๋ฐ์ดํฐ ํฉ์น๊ธฐ(๋ณํฉ) : concat(๊ฐ1, ๊ฐ2, ๊ฐ3) ํจ์ ์ฌ์ฉ
[ ์์ฑ ์ฝ๋ ]
SELECT mem_name,
CONCAT (mem_regno1, '-', mem_regno2) AS mem_regno,
CONCAT (mem_add1, ' ', mem_add2) AS mem_add
FROM member;
[ ๊ฒฐ๊ณผ ]