๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“š ์ „๊ณต ๊ณต๋ถ€/DB๊ธฐ์ดˆ

[SQL] mysql ๋ฌธ์ž์—ด ํ•จ์ˆ˜ ์ด์šฉ ์—ฐ์Šต๋ฌธ์ œ, ์˜ˆ์ œ

728x90

 

๋ฒˆ์—ญ๊ธฐ ๋Œ๋ฆฐ๊ฑฐ๋ผ ์–ด์ƒ‰ํ•œ ๋ฌธ์žฅ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค!

๊ทธ๋ž˜๋„ ์ดํ•ดํ• ๋งŒํ•จ.. ์—ฐ์Šต์šฉ์œผ๋กœ ๋Œ€์ถฉ ๋ณด๋ ค๊ณ  ์จ๋†“๋Š”๊ฑฐ๋ผ ๋ฌธ์ฒด ์ด์ƒํ•ด๋„ ๋ฌด์‹œํ•จ

chatGPT๊ฐ€ ์ œ์•ˆํ•ด์ค€ mysql ๋ฌธ์ž์—ด ํ•จ์ˆ˜ ์ด์šฉ ๋ฌธ์ œ์™€ ์˜ˆ์‹œ ๋‹ต์•ˆ์ž…๋‹ˆ๋‹ค~


๋ฌธ์ œ1 : users๋ผ๊ณ  ํ•˜๋Š” ํ…Œ์ด๋ธ”์ด ์žˆ๋Š”๋ฐ ์ปฌ๋Ÿผ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.full_name๊ฐ ์‚ฌ์šฉ์ž์˜ ์ด๋ฆ„๊ณผ ์„ฑ์„ ์ถ”์ถœํ•˜์—ฌ ๋‹ค๋ฅธ ์—ด์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉดSUBSTRING_INDEX()์—์„œ ์„ฑ๊ณผ ์ด๋ฆ„์„ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜full_name๊ธฐ๋‘ฅ. ๋‹ค์Œ์€ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค.

 
SELECT full_name, SUBSTRING_INDEX(full_name, ' ', 1) AS first_name, SUBSTRING_INDEX(full_name, ' ', -1) AS last_name
FROM users;
 

SUBSTRING_INDEX()๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„์ž๋ฅผ ์ง€์ •ํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•  ๋ฐœ์ƒ ํšŸ์ˆ˜๋ฅผ 1๋กœ ์ง€์ •ํ•˜์—ฌ ์ด๋ฆ„์„ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.๋งˆ์ง€๋ง‰ ์˜ค์นด๋ Œ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋กœ ๊ณต๋ฐฑ๊ณผ ์นด์šดํŠธ -1์„ ์ง€์ •ํ•˜์—ฌ ์„ฑ์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด, usersํ…Œ์ด๋ธ”์—๋Š” ๋‹ค์Œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 
| id | full_name |
|----|------------------|
| 1 | John Smith |
| 2 | Jane Doe |
| 3 | Robert Johnson |

์œ„์˜ ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 
| full_name | first_name | last_name |
|----------------|------------|-----------|
| John Smith       | John           | Smith    |
| Jane Doe          | Jane            | Doe      |
| Robert Johnson | Robert      | Johnson |
 
 
 
 

๋ฌธ์ œ2 : products๋ผ๊ณ  ํ•˜๋Š” ํ…Œ์ด๋ธ”์ด ์žˆ๋Š”๋ฐ ์ปฌ๋Ÿผ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.name. ๊ฐ ์ œํ’ˆ ์ด๋ฆ„์˜ ์ฒ˜์Œ 3์ž๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ์‹œํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉดUPPER()์ถ”์ถœ๋œ ์„œ๋ธŒ์ŠคํŠธ๋ง์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค.

 
 
SELECT UPPER(SUBSTRING(name, 1, 3)) AS initials
FROM products;
 

์ด ์ฟผ๋ฆฌ์—์„œ๋Š”SUBSTRING()์˜ ์ฒซ ๋ฒˆ์งธ 3๊ธ€์ž๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ธฐ๋Šฅname๊ธฐ๋‘ฅ.๊ทธ ํ›„,UPPER()์ถ”์ถœ๋œ ์„œ๋ธŒ์ŠคํŠธ๋ง์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ฉ์‹œ๋‹ค.productsํ…Œ์ด๋ธ”์—๋Š” ๋‹ค์Œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 
| id | name |
|----|-------------------|
| 1 | Apple iPhone 12 |
| 2 | Samsung Galaxy S21|
| 3 | Google Pixel 5 |

์œ„์˜ ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 
| initials |
|----------|
| APP |
| SAM |
| GOO |
 
 
 

๋ฌธ์ œ3 : orders๋ผ๊ณ  ํ•˜๋Š” ํ…Œ์ด๋ธ”์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ปฌ๋Ÿผ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.order_number์ฃผ๋ฌธ๋ฒˆํ˜ธ ํ˜•์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.YYYY-MM-DD-XXXXX,์–ด๋””์—YYYY๋…„๋„์ž…๋‹ˆ๋‹ค.MM์›”์ž…๋‹ˆ๋‹ค.DD๊ทธ๋‚ ์ด๊ณ XXXXX๋Š” ์‹œํ€€์Šค ๋ฒˆํ˜ธ์ž…๋‹ˆ๋‹ค.์‹œํ€€์Šค ๋ฒˆํ˜ธ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๋ณ„๋„์˜ ์—ด์— ํ‘œ์‹œํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉดSUBSTRING_INDEX()ํ•˜์ดํ”ˆ์ด ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐœ์ƒํ•œ ํ›„ ์ฃผ๋ฌธ ๋ฒˆํ˜ธ์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์„ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜(-)์˜ ๋ฌธ์ž์ž…๋‹ˆ๋‹ค.๋‹ค์Œ์€ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค.

 
SELECT SUBSTRING_INDEX(order_number, '-', -1) AS sequence_number
FROM orders;
 

์ด ์ฟผ๋ฆฌ์—์„œ๋Š”SUBSTRING_INDEX()๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์„ ์ถ”์ถœํ•˜๋Š” ๊ธฐ๋Šฅorder_numberํ•˜์ดํ”ˆ์ด ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐœ์ƒํ•œ ํ›„ ์ปฌ๋Ÿผ(-)์˜ ๋ฌธ์ž์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ฉ์‹œ๋‹ค.ordersํ…Œ์ด๋ธ”์—๋Š” ๋‹ค์Œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 
| id | order_number |
|----|--------------------|
| 1 | 2022-02-01-00001 |
| 2 | 2022-02-01-00002 |
| 3 | 2022-02-02-00001 |

์œ„์˜ ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 
| sequence_number |
|-----------------|
| 00001 |
| 00002 |
| 00001 |
 
 
 

๋ฌธ์ œ4 : employees๋ผ๊ณ  ํ•˜๋Š” ํ…Œ์ด๋ธ”์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ปฌ๋Ÿผ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.email์ด๋ฉ”์ผ ํ˜•์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.firstname.lastname@company.com๋„๋ฉ”์ธ ์ด๋ฆ„์„ ์ถ”์ถœํ•˜๋Š” ๊ฒฝ์šฐ(์ฆ‰,company.com๋‹ค๋ฅธ ์ปฌ๋Ÿผ์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉดSUBSTRING_INDEX()์˜ ๋งˆ์ง€๋ง‰ ๋ฐœ์ƒ ํ›„ ์ „์ž ๋ฉ”์ผ์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์„ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜( ).@)์˜ ๋ฌธ์ž์ž…๋‹ˆ๋‹ค.๋‹ค์Œ์€ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค.

 
SELECT SUBSTRING_INDEX(email, '@', -1) AS domain_name
FROM employees;
 

์ด ์ฟผ๋ฆฌ์—์„œ๋Š”SUBSTRING_INDEX()๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์„ ์ถ”์ถœํ•˜๋Š” ๊ธฐ๋Šฅemail์˜ ๋งˆ์ง€๋ง‰ ๋ฐœ์ƒ ํ›„์˜ ์ปฌ๋Ÿผ( )@)์˜ ๋ฌธ์ž์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ฉ์‹œ๋‹ค.employeesํ…Œ์ด๋ธ”์—๋Š” ๋‹ค์Œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 
| id | email |
|----|----------------------------|
| 1 | john.smith@company.com |
| 2 | jane.doe@company.com |
| 3 | robert.johnson@company.com |

์œ„์˜ ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 
| domain_name |
|--------------|
| company.com |
| company.com |
| company.com |

 

728x90