λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
DBMS/Mariadb

[DBMS]MariaDB ν•¨μˆ˜: λ°μ΄ν„°λ² μ΄μŠ€ μ‘°μž‘μ„ μœ„ν•œ κ°•λ ₯ν•œ 도ꡬ듀

by YJ Dev 2024. 5. 31.
728x90
λ°˜μ‘ν˜•
SMALL

MariaDB ν•¨μˆ˜λŠ” λ°μ΄ν„°λ² μ΄μŠ€ κ΄€λ¦¬μ—μ„œ μ€‘μš”ν•œ 역할을 μˆ˜ν–‰ν•©λ‹ˆλ‹€. 이 ν¬μŠ€νŒ…μ—μ„œλŠ” MariaDB ν•¨μˆ˜μ˜ λ‹€μ–‘ν•œ μ’…λ₯˜μ™€ ν™œμš©μ„±μ— λŒ€ν•΄ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€.

SQL ν•¨μˆ˜

κΈ°λ³Έ ν•¨μˆ˜πŸ’‘

κΈ°λ³Έ ν•¨μˆ˜λ“€μ€ λ°μ΄ν„°λ² μ΄μŠ€μ˜ 핡심적인 κΈ°λŠ₯을 μ œκ³΅ν•˜λ©°, 데이터λ₯Ό μ‘°μž‘ν•˜κ³  κ΄€λ¦¬ν•˜λŠ” 데 ν•„μˆ˜μ μž…λ‹ˆλ‹€. 잘 ν™œμš©ν•˜λ©΄ λ°μ΄ν„°λ² μ΄μŠ€μ˜ μœ μš©μ„±μ„ μ΅œλŒ€ν™”ν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ°μ΄ν„°λ² μ΄μŠ€μ˜ κ°€μž₯ 기본적인 μ‘°μž‘μ„ μœ„ν•œ ν•¨μˆ˜λ“€μ„ μ‚΄νŽ΄λ΄…λ‹ˆλ‹€. SELECT, INSERT, UPDATE, DELETE λ“±μ˜ ν•¨μˆ˜λ“€μ΄ ν¬ν•¨λ©λ‹ˆλ‹€.

⭐예제 데이터: employees1 ν…Œμ΄λΈ”

ID First Name Last Name Department Salary
1 Jane Smith Sales 55000
2 Michael Johnson HR 62000
3 Emily Brown Marketing 58000
4 David Lee Sales 60000

SELECT ν•¨μˆ˜

  • λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ 데이터λ₯Ό μ‘°νšŒν•  λ•Œ μ‚¬μš©λ˜λ©°, νŠΉμ • 쑰건에 λ§žλŠ” 데이터λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, νŠΉμ • λΆ€μ„œμ˜ λͺ¨λ“  직원을 μ„ νƒν•˜κ±°λ‚˜ νŠΉμ • 쑰건을 λ§Œμ‘±ν•˜λŠ” 데이터λ₯Ό 검색할 λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€.
SELECT `column1`, `column2` FROM `table_name` WHERE condition;

예제

SELECT * FROM `employees1` WHERE `department` = 'Sales';

μ„€λͺ…: 'employees1' ν…Œμ΄λΈ”μ—μ„œ 'Sales' λΆ€μ„œμ— μ†ν•œ λͺ¨λ“  직원을 μ„ νƒν•©λ‹ˆλ‹€.

κ²°κ³Ό

ID First Name Last Name Department Salary
1 Jane Smith Sales 55000
4 David Lee Sales 60000

INSERT ν•¨μˆ˜

  • λ°μ΄ν„°λ² μ΄μŠ€μ— μƒˆλ‘œμš΄ 데이터λ₯Ό μΆ”κ°€ν•  λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€. μƒˆλ‘œμš΄ λ ˆμ½”λ“œλ₯Ό ν…Œμ΄λΈ”μ— μ‚½μž…ν•˜κ³ , 각 열에 ν•΄λ‹Ήν•˜λŠ” 값을 지정할 수 μžˆμŠ΅λ‹ˆλ‹€.
INSERT INTO `table_name` (`column1`, `column2`) VALUES ('value1', 'value2');

예제

INSERT INTO `employees1` (`first_name`, `last_name`, `department`, `salary`) VALUES ('John', 'Doe', 'Marketing', 60000);

μ„€λͺ…: 'employees1' ν…Œμ΄λΈ”μ— μƒˆλ‘œμš΄ 직원인 John Doeλ₯Ό μΆ”κ°€ν•˜κ³ , Marketing λΆ€μ„œμ— μ†ν•˜λ©° κΈ‰μ—¬λŠ” 60000으둜 μ§€μ •ν•©λ‹ˆλ‹€.

κ²°κ³Ό

ID First Name Last Name Department Salary
1 Jane Smith Sales 55000
2 Michael Johnson HR 62000
3 Emily Brown Marketing 58000
4 David Lee Sales 60000
5 John Doe Marketing 60000

UPDATE ν•¨μˆ˜

  • λ°μ΄ν„°λ² μ΄μŠ€μ— 이미 μ‘΄μž¬ν•˜λŠ” 데이터λ₯Ό μˆ˜μ •ν•  λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€. νŠΉμ • 쑰건을 λ§Œμ‘±ν•˜λŠ” λ ˆμ½”λ“œμ˜ 값을 μ—…λ°μ΄νŠΈν•˜κ±°λ‚˜ λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
UPDATE `table_name` SET `column1` = 'value1', `column2` = 'value2' WHERE condition;

예제

UPDATE `employees1` SET `salary` = 65000 WHERE `department` = 'Marketing';

μ„€λͺ…: 'employees1' ν…Œμ΄λΈ”μ—μ„œ Marketing λΆ€μ„œμ— μ†ν•œ μ§μ›λ“€μ˜ κΈ‰μ—¬λ₯Ό 65000으둜 μ—…λ°μ΄νŠΈν•©λ‹ˆλ‹€.

κ²°κ³Ό

ID First Name Last Name Department Salary
1 Jane Smith Sales 55000
2 Michael Johnson HR 62000
3 Emily Brown Marketing 65000
4 David Lee Sales 60000
5 John Doe Marketing 65000

DELETE ν•¨μˆ˜

  • λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ νŠΉμ • 쑰건을 λ§Œμ‘±ν•˜λŠ” 데이터λ₯Ό μ‚­μ œν•  λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€. μ£Όμ˜ν•΄μ„œ μ‚¬μš©ν•΄μ•Ό ν•˜λ©°, 쑰건을 λͺ…ν™•νžˆ μ§€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ μ˜λ„μΉ˜ μ•Šμ€ 데이터 손싀이 λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€.
DELETE FROM `table_name` WHERE condition;

예제

DELETE FROM `employees1` WHERE `department` = 'HR';

μ„€λͺ…: 'employees1' ν…Œμ΄λΈ”μ—μ„œ HR λΆ€μ„œμ— μ†ν•œ λͺ¨λ“  μ§μ›λ“€μ˜ 데이터λ₯Ό μ‚­μ œν•©λ‹ˆλ‹€.

κ²°κ³Ό

ID First Name Last Name Department Salary
1 Jane Smith Sales 55000
3 Emily Brown Marketing 65000
4 David Lee Sales 60000
5 John Doe Marketing 65000

λ¬Έμžμ—΄ ν•¨μˆ˜βœ‚οΈ

λ¬Έμžμ—΄ ν•¨μˆ˜λ“€μ€ λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ λ¬Έμžμ—΄ 값을 μ²˜λ¦¬ν•˜κ³  가곡할 λ•Œ μœ μš©ν•˜κ²Œ μ‚¬μš©λ©λ‹ˆλ‹€. λ°μ΄ν„°μ˜ ν˜•μ‹μ„ μ‘°μž‘ν•˜κ±°λ‚˜ νŠΉμ • νŒ¨ν„΄μ„ μ°ΎλŠ” 데 도움이 λ©λ‹ˆλ‹€. λ¬Έμžμ—΄μ„ μ‘°μž‘ν•˜κ³  μ²˜λ¦¬ν•˜κΈ° μœ„ν•œ λ‹€μ–‘ν•œ ν•¨μˆ˜λ“€μ„ μ†Œκ°œν•©λ‹ˆλ‹€.

LENGTH ν•¨μˆ˜ λ˜λŠ” CHAR_LENGTH ν•¨μˆ˜

  • λ¬Έμžμ—΄μ˜ 길이λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
SELECT LENGTH('Hello World'); -- κ²°κ³Ό: 11
SELECT CHAR_LENGTH('μ•ˆλ…•ν•˜μ„Έμš”'); -- κ²°κ³Ό: 5

UPPER ν•¨μˆ˜

  • λ¬Έμžμ—΄μ„ λͺ¨λ‘ λŒ€λ¬Έμžλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.
SELECT UPPER('hello world'); -- κ²°κ³Ό: 'HELLO WORLD'

LOWER ν•¨μˆ˜

  • λ¬Έμžμ—΄μ„ λͺ¨λ‘ μ†Œλ¬Έμžλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.
SELECT LOWER('HELLO WORLD'); -- κ²°κ³Ό: 'hello world'

LEFT ν•¨μˆ˜

  • λ¬Έμžμ—΄μ—μ„œ μ™Όμͺ½λΆ€ν„° μ§€μ •λœ 수만큼의 문자λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
SELECT LEFT('Hello World', 5); -- κ²°κ³Ό: 'Hello'

RIGHT ν•¨μˆ˜

  • λ¬Έμžμ—΄μ—μ„œ 였λ₯Έμͺ½λΆ€ν„° μ§€μ •λœ 수만큼의 문자λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
SELECT RIGHT('Hello World', 5); -- κ²°κ³Ό: 'World'

CONCAT ν•¨μˆ˜

    • 두 개 μ΄μƒμ˜ λ¬Έμžμ—΄μ„ κ²°ν•©ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
SELECT CONCAT('Hello', ' ', 'World'); -- κ²°κ³Ό: 'Hello World'

⭐예제 데이터: employees 2 ν…Œμ΄λΈ”

id first_name last_name department salary
1 Jane Smith Sales 55000
2 Michael Johnson HR 62000
3 Emily Brown Marketing 58000
4 David Lee Sales 60000
5 John Doe Marketing 60000

예제

SELECT CONCAT(`first_name`, ' ', `last_name`) AS `full_name` FROM `employees2`;

μ„€λͺ…: 'employees2' ν…Œμ΄λΈ”μ—μ„œ 이름과 성을 κ²°ν•©ν•˜μ—¬ 전체 이름을 ν‘œμ‹œν•©λ‹ˆλ‹€.

κ²°κ³Ό

full_name
Jane Smith
Michael Johnson
Emily Brown
David Lee
John Doe

SUBSTRING ν•¨μˆ˜ λ˜λŠ” SUBSTR ν•¨μˆ˜

  • λ¬Έμžμ—΄μ—μ„œ 일뢀뢄을 μΆ”μΆœν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
SELECT SUBSTRING('Hello World', 7, 5); -- κ²°κ³Ό: 'World'
SELECT SUBSTR('Hello World', 7, 5); -- κ²°κ³Ό: 'World'

⭐예제 데이터: products ν…Œμ΄λΈ”

id description
1 Premium quality leather wallet
2 Stainless steel water bottle with insulated double wall
3 Comfortable cotton blend t-shirt for everyday wear
4 Durable nylon backpack with multiple compartments
5 Lightweight aluminum laptop stand for better ergonomics

예제

SELECT SUBSTRING(`description`, 1, 50) AS `short_description` FROM `products`;

μ„€λͺ…: 'products' ν…Œμ΄λΈ”μ—μ„œ μƒν’ˆ μ„€λͺ…μ˜ 처음 50자λ₯Ό μΆ”μΆœν•˜μ—¬ ν‘œμ‹œν•©λ‹ˆλ‹€.

κ²°κ³Ό

short_description
Premium quality leather wallet
Stainless steel water bottle with insulated double wall
Comfortable cotton blend t-shirt for everyday wear
Durable nylon backpack with multiple compartments
Lightweight aluminum laptop stand for better ergonomics

REPLACE ν•¨μˆ˜

    • λ¬Έμžμ—΄μ—μ„œ νŠΉμ • 뢀뢄을 λ‹€λ₯Έ λ¬Έμžμ—΄λ‘œ λŒ€μ²΄ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
SELECT REPLACE('Hello World', 'World', 'Universe'); -- κ²°κ³Ό: 'Hello Universe'

⭐예제 데이터: customers ν…Œμ΄λΈ”

id email
1 john@example.com
2 mary.smith@example.com
3 david_123@example.com
4 emily.lee@example.com
5 jason_k@example.com

예제

SELECT REPLACE(email, '@', '_at_') AS modified_email FROM customers;

μ„€λͺ…: 'customers' ν…Œμ΄λΈ”μ—μ„œ 이메일 μ£Όμ†Œμ—μ„œ '@'λ₯Ό 'at'둜 λŒ€μ²΄ν•˜μ—¬ ν‘œμ‹œν•©λ‹ˆλ‹€.

κ²°κ³Ό

modified_email
john_at_example.com
mary.smith_at_example.com
david_123_at_example.com
emily.lee_at_example.com
jason_k_at_example.com

TRIM ν•¨μˆ˜

  • λ¬Έμžμ—΄μ˜ μ•žλ’€μ— μžˆλŠ” 곡백을 μ œκ±°ν•©λ‹ˆλ‹€.
SELECT TRIM('   Hello World   '); -- κ²°κ³Ό: 'Hello World'

REVERSE ν•¨μˆ˜

  • λ¬Έμžμ—΄μ„ μ—­μˆœμœΌλ‘œ λ°˜ν™˜ν•©λ‹ˆλ‹€.
SELECT REVERSE('Hello World'); -- κ²°κ³Ό: 'dlroW olleH'

숫자 ν•¨μˆ˜πŸ”’

숫자 ν•¨μˆ˜λ“€μ€ λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ 숫자 값을 λ‹€μ–‘ν•œ λ°©μ‹μœΌλ‘œ μ²˜λ¦¬ν•˜κ³  μ‘°μž‘ν•  λ•Œ μœ μš©ν•˜κ²Œ μ‚¬μš©λ©λ‹ˆλ‹€. μ†Œμˆ˜μ  μ²˜λ¦¬λ‚˜ μ •μˆ˜ν™” λ“±μ˜ μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 숫자 데이터λ₯Ό 닀루기 μœ„ν•œ ν•¨μˆ˜λ“€μ„ μ‚΄νŽ΄λ΄…λ‹ˆλ‹€.

ABS ν•¨μˆ˜

  • μ ˆλŒ€κ°’μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.
SELECT ABS(-10);  -- κ²°κ³Ό: 10

ROUND ν•¨μˆ˜

    • 숫자λ₯Ό 반올림 ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. 두 번째 λ§€κ°œλ³€μˆ˜λ‘œ μ†Œμˆ˜μ  μ΄ν•˜ 자릿수λ₯Ό 지정할 수 μžˆμŠ΅λ‹ˆλ‹€.
SELECT ROUND(1.2345, 2);  -- κ²°κ³Ό: 1.23

⭐예제 데이터: employees3 ν…Œμ΄λΈ”

id first_name last_name department salary
1 Jane Smith Sales 55000.254
2 Michael Johnson HR 62000.758
3 Emily Brown Marketing 58000.503
4 David Lee Sales 60000.951
5 John Doe Marketing 60000.492

예제

SELECT ROUND(`salary`, 2) AS `rounded_salary` FROM `employees3`;

μ„€λͺ…: 'employees3 ν…Œμ΄λΈ”μ—μ„œ μ§μ›μ˜ κΈ‰μ—¬λ₯Ό μ†Œμˆ˜μ  λ‘˜μ§Έ μžλ¦¬μ—μ„œ 반올림 ν•˜μ—¬ ν‘œμ‹œν•©λ‹ˆλ‹€.

κ²°κ³Ό

rounded_salary
55000.25
62000.76
58000.50
60000.95
60000.49

CEIL ν•¨μˆ˜ λ˜λŠ” CEILING ν•¨μˆ˜

    • 숫자λ₯Ό μ˜¬λ¦Όν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. μž…λ ₯ 값보닀 ν¬κ±°λ‚˜ 같은 κ°€μž₯ μž‘μ€ μ •μˆ˜λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
SELECT CEIL(1.23);  -- κ²°κ³Ό: 2
SELECT CEILING(1.23);  -- κ²°κ³Ό: 2

⭐예제 데이터 : products ν…Œμ΄λΈ”β­

id product_name product_price
1 Leather Wallet 49.99
2 Stainless Steel Water Bottle 24.75
3 Cotton T-Shirt 15.50
4 Nylon Backpack 69.99
5 Aluminum Laptop Stand 32.45

예제

SELECT CEIL(`product_price`) AS `rounded_price` FROM `products`;

μ„€λͺ…: 'products' ν…Œμ΄λΈ”μ—μ„œ μ œν’ˆ 가격을 μ˜¬λ¦Όν•˜μ—¬ ν‘œμ‹œν•©λ‹ˆλ‹€.

κ²°κ³Ό

rounded_price
50
25
16
70
33

FLOOR ν•¨μˆ˜

  • 숫자λ₯Ό λ‚΄λ¦Όν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. μž…λ ₯ 값보닀 μž‘κ±°λ‚˜ 같은 κ°€μž₯ 큰 μ •μˆ˜λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
SELECT FLOOR(1.23);  -- κ²°κ³Ό: 1

예제

SELECT FLOOR(`product_price`) AS `rounded_price` FROM `products`;

μ„€λͺ…: 'products' ν…Œμ΄λΈ”μ—μ„œ μ œν’ˆ 가격을 λ‚΄λ¦Όν•˜μ—¬ ν‘œμ‹œν•©λ‹ˆλ‹€.

κ²°κ³Ό

rounded_price
49
24
15
69
32

λ‚ μ§œ 및 μ‹œκ°„ ν•¨μˆ˜β°

λ‚ μ§œ 및 μ‹œκ°„ ν•¨μˆ˜λ“€μ€ λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ λ‚ μ§œ 및 μ‹œκ°„ 값을 μ²˜λ¦¬ν•˜κ³  λ‹€λ£° λ•Œ μœ μš©ν•˜κ²Œ μ‚¬μš©λ©λ‹ˆλ‹€. λ‚ μ§œ κ°„μ˜ 차이λ₯Ό κ³„μ‚°ν•˜κ±°λ‚˜ λ‚ μ§œλ₯Ό ν˜•μ‹μ— 맞게 ν‘œμ‹œν•˜λŠ” λ“±μ˜ μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ‚ μ§œμ™€ μ‹œκ°„ 데이터λ₯Ό 닀루기 μœ„ν•œ ν•¨μˆ˜λ“€μ„ μ†Œκ°œν•©λ‹ˆλ‹€.

ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„

CURRENT_DATE ν•¨μˆ˜ λ˜λŠ” CURDATE ν•¨μˆ˜

  • ν˜„μž¬ λ‚ μ§œλ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. 이 ν•¨μˆ˜λŠ” μ‹œκ°„ 정보λ₯Ό μ œμ™Έν•˜κ³  ν˜„μž¬ λ‚ μ§œλ₯Ό λ‚˜νƒ€λ‚΄λŠ” DATE ν˜•μ‹μ˜ κ°’μœΌλ‘œ λ°˜ν™˜λ©λ‹ˆλ‹€.
SELECT CURRENT_DATE(); -- κ²°κ³Ό: '2024-06-04'
SELECT CURDATE(); -- κ²°κ³Ό: '2024-06-04'

CURRENT_TIME ν•¨μˆ˜λ˜λŠ” CURTIME ν•¨μˆ˜

  • ν˜„μž¬ μ‹œκ°„μ„ λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. 이 ν•¨μˆ˜λŠ” λ‚ μ§œ 정보λ₯Ό μ œμ™Έν•˜κ³  ν˜„μž¬ μ‹œκ°„μ„ λ‚˜νƒ€λ‚΄λŠ” TIME ν˜•μ‹μ˜ κ°’μœΌλ‘œ λ°˜ν™˜λ©λ‹ˆλ‹€.
SELECT CURRENT_TIME(); -- κ²°κ³Ό: '14:30:00'
SELECT CURTIME(); -- κ²°κ³Ό: '14:30:00'

CURRENT_TIMESTAMP ν•¨μˆ˜λ˜λŠ”NOW ν•¨μˆ˜

  • ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„μ„ λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. 이 ν•¨μˆ˜λŠ” λ‚ μ§œμ™€ μ‹œκ°„μ„ λͺ¨λ‘ ν¬ν•¨ν•˜λŠ” DATETIME ν˜•μ‹μ˜ κ°’μœΌλ‘œ λ°˜ν™˜λ©λ‹ˆλ‹€.
SELECT CURRENT_TIMESTAMP(); -- κ²°κ³Ό: '2024-06-04 14:30:00'
SELECT NOW(); -- κ²°κ³Ό: '2024-06-04 14:30:00'

λ‚ μ§œ 계산

DATE_ADDν•¨μˆ˜

  • λ‚ μ§œμ— 일정 기간을 λ”ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
SELECT DATE_ADD('2024-06-04', INTERVAL 10 DAY); -- κ²°κ³Ό: '2024-06-14'

⭐예제 데이터 :events ν…Œμ΄λΈ”β­

id event_name start_date
1 Conference 2024-01-15
2 Workshop 2024-02-20
3 Webinar 2024-03-25
4 Meetup 2024-04-30
5 Seminar 2024-05-10

예제

SELECT DATE_ADD(`start_date`, INTERVAL 1 MONTH) AS `new_date` FROM `events`;

μ„€λͺ…: 'events' ν…Œμ΄λΈ”μ—μ„œ μ‹œμž‘ λ‚ μ§œμ— 1κ°œμ›”μ„ λ”ν•œ μƒˆλ‘œμš΄ λ‚ μ§œλ₯Ό κ³„μ‚°ν•˜μ—¬ ν‘œμ‹œν•©λ‹ˆλ‹€.

κ²°κ³Ό

new_date
2024-02-15
2024-03-20
2024-04-25
2024-05-30
2024-06-10

DATE_SUB ν•¨μˆ˜

  • λ‚ μ§œμ— 일정 기간을 λΉΌλŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
SELECT DATE_SUB('2024-06-04', INTERVAL 10 DAY); -- κ²°κ³Ό: '2024-05-25'

예제

SELECT DATE_SUB(`start_date`, INTERVAL 1 MONTH) AS `new_date` FROM `events`;

μ„€λͺ…: 'events' ν…Œμ΄λΈ”μ—μ„œ μ‹œμž‘ λ‚ μ§œμ— 1κ°œμ›”μ„ λΊ€ μƒˆλ‘œμš΄ λ‚ μ§œλ₯Ό κ³„μ‚°ν•˜μ—¬ ν‘œμ‹œν•©λ‹ˆλ‹€.

κ²°κ³Ό

new_date
2024-12-15
2024-01-20
2024-02-25
2024-03-30
2024-04-10

λ‚ μ§œ ν˜•μ‹ λ³€ν™˜

DATE_FORMAT ν•¨μˆ˜

  • λ‚ μ§œ 및 μ‹œκ°„ 값을 μ›ν•˜λŠ” ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
SELECT DATE_FORMAT('2024-06-04 14:30:00', '%m-%d-%Y %h:%i'); -- κ²°κ³Ό: '06-04-2024 02:30'

⭐예제 데이터 : orders ν…Œμ΄λΈ”β­

id order_date
1 2024-05-31 14:30:00
2 2024-04-15 09:45:00
3 2023-12-01 16:20:00
4 2023-11-10 11:10:00
5 2023-10-05 13:30:00

예제

SELECT DATE_FORMAT(`order_date`, '%Y-%m-%d') AS `formatted_date` FROM `orders`;

μ„€λͺ…: 'orders' ν…Œμ΄λΈ”μ—μ„œ μ£Όλ¬Έ λ‚ μ§œλ₯Ό 'YYYY-MM-DD' ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•˜μ—¬ ν‘œμ‹œν•©λ‹ˆλ‹€.

κ²°κ³Ό

fomatted_date
2024-05-31
2024-04-15
2023-12-01
2023-11-10
2023-10-05

λ‚ μ§œ μΆ”μΆœ

YEAR ν•¨μˆ˜

  • λ‚ μ§œλ‚˜ datetime κ°’μ—μ„œ 연도λ₯Ό μΆ”μΆœν•©λ‹ˆλ‹€.
SELECT YEAR('2024-06-04'); -- κ²°κ³Ό: 2024

MONTH ν•¨μˆ˜

  • λ‚ μ§œλ‚˜ datetime κ°’μ—μ„œ 월을 μΆ”μΆœν•©λ‹ˆλ‹€.
SELECT MONTH('2024-06-04'); -- κ²°κ³Ό: 6

DAY ν•¨μˆ˜ λ˜λŠ” DAYOFMONTH ν•¨μˆ˜

  • λ‚ μ§œλ‚˜ datetime κ°’μ—μ„œ ν•΄λ‹Ή μ›”μ˜ λͺ‡ 번째 날인지λ₯Ό μΆ”μΆœν•©λ‹ˆλ‹€.
SELECT DAY('2024-06-04'); -- κ²°κ³Ό: 4
SELECT DAYOFMONTH('2024-06-04'); -- κ²°κ³Ό: 4

HOUR ν•¨μˆ˜

  • μ‹œκ°„ κ°’μ—μ„œ μ‹œκ°„μ„ μΆ”μΆœν•©λ‹ˆλ‹€.
SELECT HOUR('12:34:56'); -- κ²°κ³Ό: 12

MINUTE ν•¨μˆ˜

  • μ‹œκ°„ κ°’μ—μ„œ 뢄을 μΆ”μΆœν•©λ‹ˆλ‹€.
SELECT MINUTE('12:34:56'); -- κ²°κ³Ό: 34

SECOND ν•¨μˆ˜

  • μ‹œκ°„ κ°’μ—μ„œ 초λ₯Ό μΆ”μΆœν•©λ‹ˆλ‹€.
SELECT SECOND('12:34:56'); -- κ²°κ³Ό: 56

μ£Ό(day of week)와 κ΄€λ ¨λœ ν•¨μˆ˜

DAYOFWEEK ν•¨μˆ˜

  • 주어진 λ‚ μ§œμ˜ μš”μΌμ„ 숫자 ν˜•νƒœλ‘œ λ°˜ν™˜ν•©λ‹ˆλ‹€. 1은 μΌμš”μΌμ„ λ‚˜νƒ€λ‚΄κ³  7은 ν† μš”μΌμ„ λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
SELECT DAYOFWEEK('2024-06-04'); -- κ²°κ³Ό: 3 (ν™”μš”μΌ)

WEEKDAYν•¨μˆ˜

  • 주어진 λ‚ μ§œμ˜ μš”μΌμ„ 숫자 ν˜•νƒœλ‘œ λ°˜ν™˜ν•©λ‹ˆλ‹€. 0은 μ›”μš”μΌμ„ λ‚˜νƒ€λ‚΄κ³  6은 μΌμš”μΌμ„ λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
SELECT WEEKDAY('2024-06-04'); -- κ²°κ³Ό: 1 (μ›”μš”μΌ)

WEEKν•¨μˆ˜

  • 주어진 λ‚ μ§œκ°€ μ†ν•œ μ—°λ„μ˜ λͺ‡ 번째 μ£Όλ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€. 이 ν•¨μˆ˜μ˜ λ™μž‘μ€ μ„€μ •λœ μ‹œμŠ€ν…œ λ³€μˆ˜ WEEK에 따라 λ‹¬λΌμ§‘λ‹ˆλ‹€. WEEK ν•¨μˆ˜λŠ” 주의 μ‹œμž‘μ„ μ„€μ •λœ μš”μΌμ— 따라 λ‹¬λΌμ§€λ―€λ‘œ 결과도 λ‹¬λΌμ§‘λ‹ˆλ‹€.
SELECT WEEK('2024-06-04'); -- κ²°κ³Ό: 23

μΆ”κ°€ μœ μš©ν•œ ν•¨μˆ˜

STR_TO_DATE ν•¨μˆ˜

  • λ¬Έμžμ—΄μ„ λ‚ μ§œλ‚˜ μ‹œκ°„ ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€. 주어진 λ¬Έμžμ—΄μ„ νŠΉμ •ν•œ λ‚ μ§œ λ˜λŠ” μ‹œκ°„ ν˜•μ‹μœΌλ‘œ ν•΄μ„ν•˜μ—¬ λ°˜ν™˜ν•©λ‹ˆλ‹€.
SELECT STR_TO_DATE('2024-06-04', '%Y-%m-%d'); -- κ²°κ³Ό: 2024-06-04 (DATE νƒ€μž…)

μœ„μ˜ μ˜ˆμ—μ„œ %Y-%m-%dλŠ” μž…λ ₯된 λ¬Έμžμ—΄μ΄ 연도-μ›”-일 ν˜•μ‹μž„μ„ λ‚˜νƒ€λƒ…λ‹ˆλ‹€. 이와 같이 ν˜•μ‹μ„ μ§€μ •ν•˜μ—¬ μž…λ ₯된 λ¬Έμžμ—΄μ„ λ‚ μ§œ λ˜λŠ” μ‹œκ°„μœΌλ‘œ λ³€ν™˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

TIMESTAMPDIFF ν•¨μˆ˜

  • 두 λ‚ μ§œ λ˜λŠ” μ‹œκ°„ κ°„μ˜ 차이λ₯Ό κ΅¬ν•©λ‹ˆλ‹€. μ§€μ •λœ λ‹¨μœ„μ— 따라 두 λ‚ μ§œ λ˜λŠ” μ‹œκ°„ μ‚¬μ΄μ˜ 차이λ₯Ό κ³„μ‚°ν•˜μ—¬ λ°˜ν™˜ν•©λ‹ˆλ‹€.
SELECT TIMESTAMPDIFF(MONTH, '2024-01-01', '2024-06-01'); -- κ²°κ³Ό: 5 (μ›” λ‹¨μœ„)

μœ„μ˜ μ˜ˆμ—μ„œλŠ” '2024-01-01'λΆ€ν„° '2024-06-01'κΉŒμ§€μ˜ 차이λ₯Ό μ›” λ‹¨μœ„λ‘œ κ³„μ‚°ν•œ κ²°κ³Όλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

TIMESTAMPADDν•¨μˆ˜

  • 주어진 λ‚ μ§œλ‚˜ μ‹œκ°„μ— μ§€μ •λœ 간격을 λ”ν•©λ‹ˆλ‹€. μ§€μ •λœ 간격과 λ‹¨μœ„μ— 따라 λ‚ μ§œλ‚˜ μ‹œκ°„μ„ μ‘°μž‘ν•˜μ—¬ λ°˜ν™˜ν•©λ‹ˆλ‹€.
SELECT TIMESTAMPADD(DAY, 10, '2024-06-01'); -- κ²°κ³Ό: 2024-06-11 (10일을 더함)

μœ„μ˜ μ˜ˆμ—μ„œλŠ” '2024-06-01'에 10일을 λ”ν•œ κ²°κ³Όλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

쑰건 ν•¨μˆ˜πŸ§ͺ

쑰건 ν•¨μˆ˜λ“€μ€ λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ 쑰건에 따라 값을 λ™μ μœΌλ‘œ κ²°μ •ν•  λ•Œ μœ μš©ν•˜κ²Œ μ‚¬μš©λ©λ‹ˆλ‹€. μ—¬λŸ¬ 가지 쑰건을 κ³ λ €ν•˜μ—¬ 데이터λ₯Ό κ°€κ³΅ν•˜κ±°λ‚˜ μ²˜λ¦¬ν•  λ•Œ 효과적으둜 ν™œμš©λ©λ‹ˆλ‹€. 데이터 μ‘°μž‘ μ‹œ 쑰건을 μ μš©ν•˜λŠ” ν•¨μˆ˜λ“€μ„ μ‚΄νŽ΄λ΄…λ‹ˆλ‹€. IF, CASE λ“±μ˜ ν•¨μˆ˜λ“€μ΄ ν¬ν•¨λ©λ‹ˆλ‹€.

⭐예제 데이터 : employees4 ν…Œμ΄λΈ”β­

employee_id first_name last_name department salary
1 Jane Smith Sales 55000
2 Michael Johnson HR 48000
3 Emily Brown Marketing 75000
4 David Lee Sales 60000
5 John Doe Marketing 50000

IFν•¨μˆ˜

  • 쑰건에 따라 값을 λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. 쑰건식이 참이면 첫 번째 λ§€κ°œλ³€μˆ˜μ˜ 값을 λ°˜ν™˜ν•˜κ³ , 거짓이면 두 번째 λ§€κ°œλ³€μˆ˜μ˜ 값을 λ°˜ν™˜ν•©λ‹ˆλ‹€.
IF(condition, value_if_true, value_if_false)

μ—¬κΈ°μ„œ condition은 평가할 쑰건이고, 쑰건이 참이면 value_if_trueλ₯Ό λ°˜ν™˜ν•˜κ³  그렇지 μ•ŠμœΌλ©΄ value_if_falseλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

예제

SELECT `employee_id`, IF(`salary` > 50000, 'High', 'Low') AS s`alary_status` FROM `employees4`;

μ„€λͺ…: 'employees4' ν…Œμ΄λΈ”μ—μ„œ κΈ‰μ—¬κ°€ 50000보닀 큰 경우 'High'λ₯Ό, 그렇지 μ•Šμ€ 경우 'Low'λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

κ²°κ³Ό

employee_id salary_status
1 High
2 Low
3 High
4 High
5 Low

CASEν•¨μˆ˜

  • μ—¬λŸ¬ 쑰건에 따라 값을 λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. μ—¬λŸ¬ 개의 WHEN μ ˆμ„ μ‚¬μš©ν•˜μ—¬ 쑰건을 μ§€μ •ν•˜κ³ , ELSE μ ˆμ„ μ‚¬μš©ν•˜μ—¬ 기본값을 지정할 수 μžˆμŠ΅λ‹ˆλ‹€.
CASE
    WHEN condition1 THEN value1
    WHEN condition2 THEN value2
    ...
    ELSE default_value
END

μ—¬κΈ°μ„œ 각 WHEN μ ˆμ€ 쑰건을 κ²€μ‚¬ν•˜κ³ , ν•΄λ‹Ή 쑰건이 참이면 μ§€μ •λœ 값을 λ°˜ν™˜ν•©λ‹ˆλ‹€. ELSE μ ˆμ€ λͺ¨λ“  쑰건이 거짓인 경우 λ°˜ν™˜ν•  기본값을 μ§€μ •ν•©λ‹ˆλ‹€.

예제

SELECT `employee_id`, 
       CASE 
           WHEN `salary` > 70000 THEN 'High'
           WHEN `salary` > 50000 THEN 'Medium'
           ELSE 'Low'
       END AS `salary_status`
FROM `employees4`;

μ„€λͺ…: 'employees4' ν…Œμ΄λΈ”μ—μ„œ 급여에 따라 λ‹€λ₯Έ 등급을 μ§€μ •ν•©λ‹ˆλ‹€. κΈ‰μ—¬κ°€ 70000보닀 큰 경우 'High', 50000보닀 큰 경우 'Medium', 그렇지 μ•Šμ€ 경우 'Low'λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

employee_id salary_status
1 Medium
2 Low
3 High
4 Medium
5 Low

집계 ν•¨μˆ˜πŸ“Š

집계 ν•¨μˆ˜λ“€μ€ λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ κ·Έλ£Ή λ‹¨μœ„λ‘œ 데이터λ₯Ό λΆ„μ„ν•˜κ³  μš”μ•½ν•  λ•Œ μœ μš©ν•˜κ²Œ μ‚¬μš©λ©λ‹ˆλ‹€. λ°μ΄ν„°μ˜ 합계, 평균, 개수 등을 κ³„μ‚°ν•˜μ—¬ 톡계 정보λ₯Ό μ œκ³΅ν•  λ•Œ 효과적으둜 ν™œμš©λ©λ‹ˆλ‹€. 데이터 그룹을 λŒ€μƒμœΌλ‘œ 연산을 μˆ˜ν–‰ν•˜λŠ” ν•¨μˆ˜λ“€μ„ μ‚΄νŽ΄λ΄…λ‹ˆλ‹€. SUM, AVG, COUNT λ“±μ˜ ν•¨μˆ˜λ“€μ΄ 여기에 ν¬ν•¨λ©λ‹ˆλ‹€.

⭐예제 데이터 :employees4ν…Œμ΄λΈ”β­

employee_id first_name lst_name department salary age
1 Jane Smith Sales 55000 30
2 Michael Johnson HR 48000 45
3 Emily Brown Marketing 75000 29
4 David Lee Sales 60000 35
5 John Doe Marketing 50000 40
6 Anna Taylor HR 52000 50
7 James Wilson Sales 57000 25
8 Laura Davis Marketing 68000 32

SUM ν•¨μˆ˜

  • 주어진 μ—΄μ˜ 값을 λͺ¨λ‘ ν•©μ‚°ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
SELECT SUM(`column_name`) FROM `table_name`;

예제

SELECT `department`, SUM(`salary`) AS `total_salary` FROM `employees5` GROUP BY `department`;

μ„€λͺ…: 'employees5' ν…Œμ΄λΈ”μ—μ„œ λΆ€μ„œλ³„ μ§μ›μ˜ κΈ‰μ—¬ 합계λ₯Ό κ³„μ‚°ν•©λ‹ˆλ‹€.

κ²°κ³Ό

department total_salary
Sales 172000
HR 100000
Marketing 193000

AVG ν•¨μˆ˜

  • 주어진 μ—΄μ˜ κ°’λ“€μ˜ 평균을 κ³„μ‚°ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
SELECT AVG(`column_name`) FROM `table_name`;

예제

SELECT `department`, AVG(`age`) AS `average_age` FROM `employees5` GROUP BY `department`;

μ„€λͺ…: 'employees5' ν…Œμ΄λΈ”μ—μ„œ λΆ€μ„œλ³„ μ§μ›μ˜ 평균 λ‚˜μ΄λ₯Ό κ³„μ‚°ν•©λ‹ˆλ‹€.

κ²°κ³Ό

department average_age
Sales 30
HR 47.5
Marketing 33.67

COUNTν•¨μˆ˜

  • 주어진 쑰건에 λ§žλŠ” ν–‰μ˜ 개수λ₯Ό μ„ΈλŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
SELECT COUNT(`column_name`) FROM `table_name`;

예제

SELECT `department`, COUNT(*) AS `employee_count` FROM `employees5` GROUP BY `department`;

μ„€λͺ…: 'employees5' ν…Œμ΄λΈ”μ—μ„œ λΆ€μ„œλ³„ 직원 수λ₯Ό κ³„μ‚°ν•©λ‹ˆλ‹€.

κ²°κ³Ό

department employee_count
Sales 3
HR 2
Marketing 3

핡심 λ‚΄μš©πŸ‘€

SQL ν•¨μˆ˜
SQL ν•¨μˆ˜
SQL ν•¨μˆ˜
SQL ν•¨μˆ˜
SQL ν•¨μˆ˜
SQL ν•¨μˆ˜

728x90
λ°˜μ‘ν˜•