๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT/Database

[Database] Heidi SQL ๋ฌธ์ž์—ดํ•จ์ˆ˜, Select, From, Where, ๋ณ„์นญ ์‚ฌ์šฉ, ์ •๋ ฌ, ์—ฐ์‚ฐ์ž, ๋‚ ์งœ ํ˜•์‹, ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ˆœ์„œ

by ITyranno 2023. 11. 24.
728x90
๋ฐ˜์‘ํ˜•

 

 

 

 

 

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค.

 

 

 

 

 

 

ํšŒ์› ํ…Œ์ด๋ธ” ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ

 

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;

 

 

[ ๊ฒฐ๊ณผ ]

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•

loading