ํ๋ก๊ทธ๋๋ฐ ์ธ๊ณ๋ฅผ ํ๊ตฌํฉ์๋ค.
[ ์กฐ๊ฑด ]
-- [๋ฌธ์ ]
-- ์ํ๋ถ๋ฅ๋ช
์ ์ปดํจํฐ๊ฐ ํฌํจ๋์ด ์๊ณ ,
-- ์ฃผ๋ฌธ์ด๋ ฅ์ด ์๋ ์ํ์ ๋ํ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ค๊ณ ํฉ๋๋ค.
-- ์ํ๋ช
, ์ํ๋ถ๋ฅ๋ช
์กฐํํ๊ธฐ
[ ์์ฑ ์ฝ๋ ]
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;
[ ๊ฒฐ๊ณผ ]