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

[Database] HeidiSQL ํ•จ์ˆ˜ In, Replace, Select, nvl, ๋‚ ์งœ ํ•จ์ˆ˜

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

 

 

 

 

 

 

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

 

 

 

 

 

 

[  ์กฐ๊ฑด  ]

 

-- [๋ฌธ์ œ]
-- ์ƒํ’ˆ๋ถ„๋ฅ˜๋ช…์— ์ปดํ“จํ„ฐ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ณ ,
-- ์ฃผ๋ฌธ์ด๋ ฅ์ด ์žˆ๋Š” ์ƒํ’ˆ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
-- ์ƒํ’ˆ๋ช…, ์ƒํ’ˆ๋ถ„๋ฅ˜๋ช… ์กฐํšŒํ•˜๊ธฐ

 

 

 

[  ์ž‘์„ฑ ์ฝ”๋“œ  ]

 

SELECT Prod_name, 
(select Lprod_nm FROM lprod WHERE lprod_gu=prod_lgu) AS lprod_nm
FROM prod
WHERE prod_lgu IN (SELECT lprod_gu FROM lprod WHERE lprod_nm LIKE '%์ปดํ“จํ„ฐ%')
 AND prod_id IN (SELECT cart_prod FROM cart);

 

 

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

 

1. Replace ํ•จ์ˆ˜

 

์น˜ํ™˜ํ•˜๊ธฐ

replace(์›๋ณธ๊ฐ’, ์ฐพ์„ ๊ฐ’, ๋ฐ”๊ฟ€ ๊ฐ’)

 

 

[  ํ™œ์šฉ ์˜ˆ์‹œ  ]

 

 -- Replace() : ์น˜ํ™˜ํ•˜๊ธฐ
 SELECT REPLACE ('์ปฌ๋Ÿผ1์— ์ฐพ์„ ๊ฐ’์ด ์—†์–ด์š”', '์ฐพ์„๊ฐ’', '๋ฐ”๊ฟ€ ๊ฐ’') AS msg;

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

 

[  ์กฐ๊ฑด  ]

 

-- ํšŒ์›์˜ ์„ฑ์”จ ์ค‘์— ์ด์”จ๋ฅผ ๋ฆฌ์”จ๋กœ ๋ฐ”๊ฟ”์ฃผ์„ธ์š”
 -- ์„ฑ๋งŒ ๋ฐ”๊ฟ”์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
 -- ์˜ˆ์‹œ : ์ด์ˆœ์‹  -> ๋ฆฌ์ˆœ์‹ 

 

 

 

[  ์ž‘์„ฑ ์ฝ”๋“œ1  ]

 

 

 SELECT
 concat(REPLACE(SUBSTRING(mem_name,1,1), '์ด', '๋ฆฌ'), SUBSTRING(mem_name ,2))
 FROM member
 ;

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

[  ์ž‘์„ฑ ์ฝ”๋“œ 2 ]

 

 

 SELECT mem_name,
 			REPLACE(mem_name, '์ด', '๋ฆฌ') AS rep1,
 			REPLACE(substring(mem_name, 1, 1), '์ด', '๋ฆฌ') AS rep2,
 			SUBSTRING(mem_name, 2, 2) AS nm,
 			CONCAT(REPLACE(substring(mem_name, 1, 1), '์ด', '๋ฆฌ'),
			 SUBSTRING(mem_name, 2, 2)) AS rep_name
 FROM member
 WHERE SUBSTRING(mem_name, 1, 1) = '์ด';

 

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

 

 

 

1. Round ํ•จ์ˆ˜

 

๋ฐ˜์˜ฌ๋ฆผ ํ•จ์ˆ˜

round(๋ฐ˜์˜ฌ๋ฆผ ํ•  ์ˆซ์ž, ์†Œ์ˆซ์  ์•„๋ž˜ ๋‚จ๊ธธ ์ˆซ์ž ๊ฐฏ์ˆ˜)

 

 

[  ํ™œ์šฉ ์˜ˆ์‹œ ]

 

 

 -- Round() : ๋ฐ˜์˜ฌ๋ฆผ ํ•จ์ˆ˜
 SELECT ROUND(123.567, 0) AS r1,
        ROUND(123.567, 1) AS r2,
  		  ROUND(123.567, 2) AS r3,
  		  ROUND(123.567, 3) AS r4,
        ROUND(123.567, 4) AS r5,
		  ROUND(123.567, -1) AS r6,
		  ROUND(123.567, -2) AS r7,
		  ROUND(123.567, -3) AS r8,
		  ROUND(123.567, -4) AS r9;

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

 

[  ์กฐ๊ฑด  ]

 

-- [ ๋ฌธ์ œ ]
-- ์ƒํ’ˆ์ •๋ณด์—์„œ ์ƒํ’ˆ๋ช…, ์›๊ฐ€์œจ์„ ์กฐํšŒํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
-- ์›๊ฐ€์œจ = ๋งค์ž…๊ฐ€/ํŒ๋งค๊ฐ€์˜ ๋ฐฑ๋ถ„์œจ ๊ฐ’์ž…๋‹ˆ๋‹ค.
-- ์›๊ฐ€์œจ์€ ์†Œ์ˆซ์  2์ž๋ฆฌ๊นŒ์ง€ ํ‘œํ˜„

 

 

 

[  ์ž‘์„ฑ ์ฝ”๋“œ  ]

 

 

SELECT prod_name, round(Prod_cost/Prod_sale*100, 2)
FROM prod
;

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

 

[  ์กฐ๊ฑด  ]

 

 

-- ํšŒ์›์ค‘์— ์ทจ๋ฏธ๊ฐ€ ์ˆ˜์˜์ธ ํšŒ์›์ด ๊ตฌ๋งคํ•œ ์ƒํ’ˆ๋“ค์„ ์กฐํšŒํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค
-- ํšŒ์› ์ง€์—ญ์ด ์„œ์šธ, ๋Œ€์ „, ๊ด‘์ฃผ์— ๊ฑฐ์ฃผํ•˜๋Š” ํšŒ์›๋“ค
-- ์ƒํ’ˆ๋ถ„๋ฅ˜ ์ค‘์— ํ”ผํ˜์ด๋ผ๋Š” ๋ถ„๋ฅ˜์— ์†ํ•ด ์žˆ๋Š” ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•œ ํšŒ์›
-- ์กฐํšŒ์ปฌ๋Ÿผ, ์ƒํ’ˆ๋ช…, ์ƒํ’ˆ๋ถ„๋ฅ˜๋ช…, ์›๊ฐ€(์†Œ์ˆ˜์  2์ž๋ฆฌ๊นŒ์ง€)

 

 

 

[  ์ž‘์„ฑ ์ฝ”๋“œ  ]

 

SELECT prod_name, round(Prod_cost/Prod_sale*100, 2) AS mm,
(select Lprod_nm FROM lprod WHERE lprod_gu=prod_lgu) AS lprod_nm
FROM prod
WHERE
prod_id IN (SELECT cart_prod FROM cart 
WHERE cart_member IN(SELECT mem_id FROM member WHERE mem_like LIKE '%์ˆ˜์˜%'
AND
SUBSTRING(mem_add1, 1, 2) IN ('์„œ์šธ', '๋Œ€์ „', '๊ด‘์ฃผ')))
AND
prod_lgu IN(SELECT lprod_gu FROM lprod WHERE lprod_nm LIKE '%ํ”ผํ˜%');

 

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

 

 

[  CASE๋ฌธ ์ž‘์„ฑ ๋ฐฉ๋ฒ•  ]

 

 

-- ์กฐ๊ฑด ์ปฌ๋Ÿผ ๋Œ€์‹  [์กฐ๊ฑด๋ฌธ]์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ

 

case ๊ธฐ์ค€๊ฐ’
when ๋น„๊ต๊ฐ’ then ์ฒ˜๋ฆฌํ•  ๊ฐ’
else ์ฒ˜๋ฆฌํ•  ๊ฐ’
end

case ๊ธฐ์ค€๊ฐ’
when ๋น„๊ต์—ฐ์‚ฐ์ž then ์ฒ˜๋ฆฌํ•  ๊ฐ’
when ๋น„๊ต์—ฐ์‚ฐ์ž then ์ฒ˜๋ฆฌํ•  ๊ฐ’
else ์ฒ˜๋ฆฌํ•  ๊ฐ’
end

 

 

 

[  ํ™œ์šฉ ์˜ˆ์‹œ  1  ]

 

/*
case ๊ธฐ์ค€๊ฐ’
	when ๋น„๊ต๊ฐ’ then ์ฒ˜๋ฆฌํ•  ๊ฐ’
	else ์ฒ˜๋ฆฌํ•  ๊ฐ’
end
*/

SELECT case 1
		when 1 then '1์ž…๋‹ˆ๋‹ค'
		when 0 then '0์ž…๋‹ˆ๋‹ค'
		ELSE '์Œ์ˆ˜'
		END AS case_data;

 

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

[  ํ™œ์šฉ ์˜ˆ์‹œ  2  ]

 

 

/*
case ๊ธฐ์ค€๊ฐ’
	when ๋น„๊ต์—ฐ์‚ฐ์ž then ์ฒ˜๋ฆฌํ•  ๊ฐ’
	when ๋น„๊ต์—ฐ์‚ฐ์ž then ์ฒ˜๋ฆฌํ•  ๊ฐ’
	else ์ฒ˜๋ฆฌํ•  ๊ฐ’
end
*/
		
SELECT case
		when 1>0 then '1์ž…๋‹ˆ๋‹ค'
		when 1<0 then '0์ž…๋‹ˆ๋‹ค'
		ELSE '์Œ์ˆ˜'
		END AS case_data;

 

 

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

 

[  ์กฐ๊ฑด  ]

 

 

-- [๋ฌธ์ œ]
-- ํšŒ์›์•„์ด๋””, ํšŒ์›์ด๋ฆ„, ์„ฑ๋ณ„์„ ์กฐํšŒํ•ด ์ฃผ์„ธ์š”

 

 

 

[  ์ž‘์„ฑ ์ฝ”๋“œ  ]

 

 

-- [๋ฌธ์ œ]
-- ํšŒ์›์•„์ด๋””, ํšŒ์›์ด๋ฆ„, ์„ฑ๋ณ„์„ ์กฐํšŒํ•ด ์ฃผ์„ธ์š”
SELECT Mem_id, Mem_name,
		(case substring(Mem_regno2, 1, 1)
		when 1 then '๋‚จ'
		when 3 then '๋‚จ'
		when 2 then '์—ฌ'
		when 4 then '์—ฌ'
		ELSE '์˜ค๋ฅ˜'
		END) AS ์„ฑ๋ณ„
FROM member;

 

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

 

 

 

[  ์กฐ๊ฑด  ]

 

 

-- [๋ฌธ์ œ]
-- ํšŒ์›์ด๋ฆ„, ํšŒ์›์„ฑ๋ณ„, ๋งˆ์ผ๋ฆฌ์ง€, ๊ณ ๊ฐ๊ตฌ๋ถ„
-- ๊ณ ๊ฐ๊ตฌ๋ถ„์€ ๋งˆ์ผ๋ฆฌ์ง€์˜ ๊ฐ’์ด 5000 ์ด์ƒ์ด๋ฉด '์šฐ์ˆ˜๊ณ ๊ฐ'
--								  		5000 ๋ฏธ๋งŒ์ด๋ฉด '๋ธ”๋ž™๋ฆฌ์ŠคํŠธ'

 

 

 

[  ์ž‘์„ฑ ์ฝ”๋“œ  1  - case ๋ฌธ  ]

 

 

SELECT Mem_name, 
(case substring(Mem_regno2, 1, 1)
		when 1 then '๋‚จ'
		when 3 then '๋‚จ'
		when 2 then '์—ฌ'
		when 4 then '์—ฌ'
		ELSE '์˜ค๋ฅ˜'
		END) AS ์„ฑ๋ณ„, Mem_mileage,
if (Mem_mileage >= 5000, '์šฐ์ˆ˜๊ณ ๊ฐ', '๋ธ”๋ž™๋ฆฌ์ŠคํŠธ') AS ๊ณ ๊ฐ๊ตฌ๋ถ„
FROM member;

 

 

 

[  ์ž‘์„ฑ ์ฝ”๋“œ  2  -  if๋ฌธ  ]

 

 

SELECT Mem_name, 
		if(MOD(SUBSTRING(mem_regno2, 1, 1), 2)>0, '๋‚จ', '์—ฌ') AS ์„ฑ๋ณ„, Mem_mileage,
if (Mem_mileage >= 5000, '์šฐ์ˆ˜๊ณ ๊ฐ', '๋ธ”๋ž™๋ฆฌ์ŠคํŠธ') AS ๊ณ ๊ฐ๊ตฌ๋ถ„
FROM member;

 

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

 

 

 

[  ์กฐ๊ฑด  ]

 

 

-- [๋ฌธ์ œ]
-- ํšŒ์›์ด ๊ตฌ๋งคํ•œ ์ƒํ’ˆ๋ช…, ํŒ๋งค๊ฐ€๊ฒฉ์„ ์กฐํšŒํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
-- ๋‹จ, ์—ฌ์„ฑ์ธ ํšŒ์›๋“ค์ด ๊ตฌ๋งคํ•œ ์ƒํ’ˆ์ด๊ณ ,
--      ์šฐ์ˆ˜๊ณ ๊ฐ์ด ๊ตฌ๋งคํ•œ ์ƒํ’ˆ์— ๋Œ€ํ•ด์„œ๋งŒ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
-- ์šฐ์ˆ˜๊ณ ๊ฐ์€ ๋งˆ์ผ๋ฆฌ์ง€๊ฐ’์ด 5000์  ์ด์ƒ์ธ ํšŒ์›์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

 

 

[  ์ž‘์„ฑ ์ฝ”๋“œ  ]

 

 

SELECT prod_name, prod_sale
FROM prod
WHERE prod_id IN (
		SELECT cart_prod
		FROM cart
		WHERE cart_member IN(
		SELECT mem_id
		FROM member
		WHERE mod(substring(mem_regno2, 1, 1), 2)=0
		AND mem_mileage >= 5000));

 

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

 

 

 

 

 

[  nvl  ]

 

-- null, Nan, None : null ์ฒดํฌ -> nvl(null, 'null์ธ๊ฒฝ์šฐ๊ฐ’')

 

-- nvl(์ปฌ๋Ÿผ๋ช…, null์ธ ๊ฒฝ์šฐ ๋Œ€์ฒดํ•  ๊ฐ’)

 

 

[  ํ™œ์šฉ ์˜ˆ์‹œ  ]

 

-- null, Nan, None : null ์ฒดํฌ -> nvl(null, 'null์ธ๊ฒฝ์šฐ๊ฐ’')
-- nvl(์ปฌ๋Ÿผ๋ช…, null์ธ ๊ฒฝ์šฐ ๋Œ€์ฒดํ•  ๊ฐ’)
SELECT nvl(NULL, 'null์ด๋„ค์š”'),
		 nvl(1, 'null์ด๋„ค์š”');

 

 

 

 

 

 

 

 

 

 

[  ๋‚ ์งœ ํ•จ์ˆ˜  ]

 

-- now() : ๋ฐ์ดํ„ฐ insert ์‹œ์— ์ฃผ๋กœ ์‚ฌ์šฉ
-- curdate() : ๋ฐ์ดํ„ฐ insert์‹œ์— ๋…„์›”์ผ๋งŒ ๋„ฃ๊ณ ์ž ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ

 

 

[  ํ™œ์šฉ ์˜ˆ์‹œ  ]

 

SELECT NOW() AS '์˜ค๋Š˜๋‚ ์งœ', CURDATE() AS '์˜ค๋Š˜ ๋…„์›”์ผ',
		CURTIME() AS '์˜ค๋Š˜ ์‹œ๋ถ„์ดˆ';

 

 

 

 

 

 

[  ๋‚ ์งœ ํฌ๋งท ์ด์šฉํ•˜๊ธฐ  ]

 

SELECT DATE_FORMAT('20231122144059','%Y-%m-%d %H:%i:%s') AS dt1,
		 DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') AS dt2;

 

 

 

 

 

 

 

 

[  ์กฐ๊ฑด  ]

 

 

-- ํšŒ์›์•„์ด๋””, ํšŒ์›์ด๋ฆ„, ํšŒ์›์ƒ์ผ, ์ฃผ๋ฏผ๋ฒˆํ˜ธ ์•ž์ž๋ฆฌ ์กฐํšŒํ•˜๊ธฐ
-- ์ƒ์ผ, ์ฃผ๋ฏผ๋ฒˆํ˜ธ๋Š” '๋…„-์›”-์ผ' ํ˜•ํƒœ๋กœ ์กฐํšŒ

 

 

 

[  ์ž‘์„ฑ ์ฝ”๋“œ  ]

 

 

SELECT Mem_id, Mem_name, 
			DATE_FORMAT(Mem_bir,'%Y-%m-%d') AS mem_bir,
			DATE_FORMAT(Mem_regno1,'%Y-%m-%d') AS mem_regno1
FROM member;

 

 

 

 

[  ๊ฒฐ๊ณผ  ]

 

 

728x90
๋ฐ˜์‘ํ˜•

loading