Scalar Funtions
- 입력값을 기준으로 단일 값을 반환 하는 함수

1. ucase(대문자) & lcase(소문자)
mysql> select ucase('This is ucase test.');
+
| ucase('This is ucase test.') |
+
| THIS IS UCASE TEST. |
+
1 row in set (0.01 sec)
mysql> select ucase(menu) from sandwich where price > 15;
+
| ucase(menu) |
+
| LOBSTER ROLL |
| GRILLED LAUGHING BIRD SHRIMP AND |
| SHAVED PRIME RIB |
+
3 rows in set (0.01 sec)
mysql> select lcase(menu) from sandwich where price >15;
+
| lcase(menu) |
+
| lobster roll |
| grilled laughing bird shrimp and |
| shaved prime rib |
+
3 rows in set (0.02 sec)
2. MID(대상값, 시작, 끝)
mysql> select mid("This is mid test.", 1, 4);
+
| mid("This is mid test.", 1, 4) |
+
| This |
+
1 row in set (0.02 sec)
mysql> select mid("This is mid test.", 6, 5);
+
| mid("This is mid test.", 6, 5) |
+
| is mi |
+
1 row in set (0.01 sec)
mysql> select mid("This is mid test.", -4, 4);
+
| mid("This is mid test.", -4, 4) |
+
| est. |
+
1 row in set (0.01 sec)
mysql> select cafe from sandwich where ranking=11;
+
| cafe |
+
| Lula Cafe |
+
1 row in set (0.02 sec)
mysql> select mid(cafe, 6, 4) from sandwich where ranking=11;
+
| mid(cafe, 6, 4) |
+
| Cafe |
+
1 row in set (0.01 sec)
mysql> select mid(cafe, -4, 4) from sandwich where ranking=11;
+
| mid(cafe, -4, 4) |
+
| Cafe |
+
1 row in set (0.01 sec)
3. Length
- 문자열 길이를 반환하는 함수
- 공백(' ')의 길이는 1, null의 길이는 그대로 null 반환
mysql> select length('This is len test.');
+
| length('This is len test.') |
+
| 17 |
+
1 row in set (0.17 sec)
mysql> select length('');
+
| length('') |
+
| 0 |
+
1 row in set (0.06 sec)
mysql> select length(' ');
+
| length(' ') |
+
| 1 |
+
1 row in set (0.01 sec)
mysql> select length(null);
+
| length(null) |
+
| NULL |
+
1 row in set (0.02 sec)
mysql> select ranking, address, length(address) from sandwich
-> where ranking < 4;
+
| ranking | address | length(address) |
+
| 1 | 2109 W. Chicago Ave | 19 |
| 2 | 800 W. Randolph St | 18 |
| 3 | 445 N. Clark St | 16 |
+
3 rows in set (0.02 sec)
4. Round
- 지정한 자리에서 숫자를 반올림하는 함수
- round(대상숫자, 반올림할 n번째 자릿수)
- 디폴트 0: 소수점 첫번쨰 자리에서 반올림
mysql> select round(123.987, 1);
+
| round(123.987, 1) |
+
| 124.0 |
+
1 row in set (0.00 sec)
mysql> select round(123.987, -1);
+
| round(123.987, -1) |
+
| 120 |
+
1 row in set (0.01 sec)
mysql> select ranking, price, round(price)
-> from sandwich
-> order by ranking desc
-> limit 3;
+
| ranking | price | round(price) |
+
| 50 | 6.85 | 7 |
| 49 | 8.75 | 9 |
| 48 | 7.5 | 8 |
+
3 rows in set (0.02 sec)
5. now
mysql> select now();
+
| now() |
+
| 2024-08-08 12:17:58 |
+
1 row in set (0.07 sec)
- 숫자를 천단위 콤마가 있는 형식으로 반환하는 함수
mysql> select format(12345678.9876, 0);
+
| format(12345678.9876, 0) |
+
| 12,345,679 |
+
1 row in set (0.01 sec)
mysql> select format(12345678.9876, 2);
+
| format(12345678.9876, 2) |
+
| 12,345,678.99 |
+
1 row in set (0.01 sec)
mysql> select format(12345678.9876, 10);
+
| format(12345678.9876, 10) |
+
| 12,345,678.9876000000 |
+
1 row in set (0.03 sec)
mysql> select format(가격, 0) from oil_price
-> where round(가격, -3) >= 2000;
+
| format(가격, 0) |
+
| 1,509 |
| 1,598 |
| 1,635 |
| 2,160 |
+
4 rows in set (0.01 sec)
전체 실습 문제 풀이
1. sandwich 테이블에서 가게 이름은 대문자, 메뉴 이름은 소문자로 조회
mysql> select ucase(cafe), lcase(menu) from sandwich limit 5;
+
| ucase(cafe) | lcase(menu) |
+
| OLD OAK TAP | blt |
| AU CHEVAL | fried bologna |
| XOCO | woodland mushroom |
| AL’S DELI | roast beef |
| PUBLICAN QUALITY MEATS | pb&l |
+
5 rows in set (0.02 sec)
2. sandwich 테이블에서 10위 메뉴의 마지막 단어를 조회하세요.
mysql> select ranking, cafe, mid(menu, -3, 3) from sandwich where ranking=10;
+
| ranking | cafe | mid(menu, -3, 3) |
+
| 10 | Nana | and |
+
1 row in set (0.01 sec)
3. sandwich 테이블에서 메뉴 이름의 평균 길이를 조회하세요.
mysql> select avg(length(menu)) from sandwich;
+
| avg(length(menu)) |
+
| 13.9600 |
+
1 row in set (0.01 sec)
4. oil_price 테이블에서 가격을 십원 단위에서 반올림하여 조회하세요.
mysql> select 상호, 가격, round(가격, -2) from oil_price;
+
| 상호 | 가격 | round(가격, -2) |
+
| 타이거주유소 | 1484 | 1500 |
| (주)명연에너지 | 1485 | 1500 |
| 성락주유소 | 1498 | 1500 |
| (주)MS주유소 | 1498 | 1500 |
| 쌍문주유소 | 1509 | 1500 |
| 21세기주유소 | 1598 | 1600 |
| 살피재주유소 | 1635 | 1600 |
| 뉴서울(강남) | 2160 | 2200 |
| 신길주유소 | 1498 | 1500 |
+
9 rows in set (0.08 sec)
5. oil_price 테이블에서 가격이 십원 단위에서 반올림했을 때, 2000원 이상인 경우, 천 단위에 콤마를 넣어서 조회하세요.
mysql> select 상호, format(가격, 0) from oil_price
-> where round(가격, -2) >= 2000;
+
| 상호 | format(가격, 0) |
+
| 뉴서울(강남) | 2,160 |
+
1 row in set (0.01 sec)