MariaDB ν¨μλ λ°μ΄ν°λ² μ΄μ€ κ΄λ¦¬μμ μ€μν μν μ μνν©λλ€. μ΄ ν¬μ€ν μμλ MariaDB ν¨μμ λ€μν μ’ λ₯μ νμ©μ±μ λν΄ μμλ³΄κ² μ΅λλ€.
β£ λͺ©μ°¨
κΈ°λ³Έ ν¨μπ‘
κΈ°λ³Έ ν¨μλ€μ λ°μ΄ν°λ² μ΄μ€μ ν΅μ¬μ μΈ κΈ°λ₯μ μ 곡νλ©°, λ°μ΄ν°λ₯Ό μ‘°μνκ³ κ΄λ¦¬νλ λ° νμμ μ λλ€. μ νμ©νλ©΄ λ°μ΄ν°λ² μ΄μ€μ μ μ©μ±μ μ΅λνν μ μμ΅λλ€. λ°μ΄ν°λ² μ΄μ€μ κ°μ₯ κΈ°λ³Έμ μΈ μ‘°μμ μν ν¨μλ€μ μ΄ν΄λ΄ λλ€. 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 | |
---|---|
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 |
ν΅μ¬ λ΄μ©π