Function | Description |
---|---|
UCASE | 영문을 대문자로 변환하는 함수 |
LCASE | 영문을 소문자로 변환하는 함수 |
MID | 문자열 부분을 반환하는 함수 |
LENGTH | 문자열의 길이를 반환하는 함수 |
ROUND | 지정한 자리에서 숫자를 반올림하는 함수 (0이 소수점 첫째 자리) |
NOW | 현재 날짜 및 시간을 반환하는 함수 |
FORMAT | 숫자를 천단위 콤마가 있는 형식으로 반환하는 함수 |
import mysql.connector
import pandas as pd
conn = mysql.connector.connect(
host = "database-1.ca0oa5o01l8m.ap-northeast-1.rds.amazonaws.com",
port = 3306,
user = "zero",
password = "zerobase",
database = "zerobase"
)
df = pd.read_csv("./sandwich.csv", encoding="utf-8")
df['Price'] = df['Price'].str.replace(pat=r'^[$]', repl=r'', regex=True).astype("float")
df.info()
=>
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 50 entries, 0 to 49
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Rank 50 non-null int64
1 Cafe 50 non-null object
2 Menu 50 non-null object
3 Price 50 non-null float64
4 Address 50 non-null object
dtypes: float64(1), int64(1), object(3)
memory usage: 2.1+ KB
sql = "create table sandwich (ranking int, cafe varchar(32), menu varchar(32), price float, address varchar(32))"
cursor = conn.cursor(buffered=True)
cursor.execute(sql)
cursor.execute("desc sandwich")
result = cursor.fetchall()
for row in result:
print(row)
=>
('ranking', b'int', 'YES', '', None, '')
('cafe', b'varchar(32)', 'YES', '', None, '')
('menu', b'varchar(32)', 'YES', '', None, '')
('price', b'float', 'YES', '', None, '')
('address', b'varchar(32)', 'YES', '', None, '')
sql = "insert into sandwich values (%s, %s, %s, %s, %s)"
for i, row in df.iterrows():
cursor.execute(sql, tuple(row))
print(tuple(row))
conn.commit()
conn.close()
=>
(1, 'Old Oak Tap', 'BLT', 10.0, '2109 W. Chicago Ave')
(2, 'Au Cheval', 'Fried Bologna', 9.0, '800 W. Randolph St')
(3, 'Xoco', 'Woodland Mushroom', 9.5, ' 445 N. Clark St')
...
(48, 'Zenwich', 'Beef Curry', 7.5, ' 416 N. York St')
(49, 'Toni Patisserie', 'Le Végétarien', 8.75, ' 65 E. Washington St')
(50, 'Phoebe’s Bakery', 'The Gatsby', 6.85, ' 3351 N. Broadwa')
mysql> select * from sandwich;
+---------+---------------------------------+----------------------------------+-------+-------------------------+
| ranking | cafe | menu | price | address |
+---------+---------------------------------+----------------------------------+-------+-------------------------+
| 1 | Old Oak Tap | BLT | 10 | 2109 W. Chicago Ave |
| 2 | Au Cheval | Fried Bologna | 9 | 800 W. Randolph St |
| 3 | Xoco | Woodland Mushroom | 9.5 | 445 N. Clark St |
| 4 | Al’s Deli | Roast Beef | 9.4 | 914 Noyes St |
| 5 | Publican Quality Meats | PB&L | 10 | 825 W. Fulton Mkt |
| 6 | Hendrickx Belgian Bread Crafter | Belgian Chicken Curry Salad | 7.25 | 100 E. Walton St |
| 7 | Acadia | Lobster Roll | 16 | 1639 S. Wabash Ave |
| 8 | Birchwood Kitchen | Smoked Salmon Salad | 10 | 2211 W. North Ave |
| 9 | Cemitas Puebla | Atomica Cemitas | 9 | 3619 W. North Ave |
| 10 | Nana | Grilled Laughing Bird Shrimp and | 17 | 3267 S. Halsted St |
| 11 | Lula Cafe | Ham and Raclette Panino | 11 | 2537 N. Kedzie Blvd |
| 12 | Ricobene’s | Breaded Steak | 5.49 | Multiple location |
| 13 | Frog n Snail | The Hawkeye | 14 | 3124 N. Broadwa |
| 14 | Crosby’s Kitchen | Chicken Dip | 10 | 3455 N. Southport Ave |
| 15 | Longman & Eagle | Wild Boar Sloppy Joe | 13 | 2657 N. Kedzie Ave |
| 16 | Bari | Meatball Sub | 4.5 | 1120 W. Grand Ave |
| 17 | Manny’s | Corned Beef | 11.95 | 1141 S. Jefferson St |
| 18 | Eggy’s | Turkey Club | 11.5 | 333 E. Benton Pl |
| 19 | Old Jerusalem | Falafel | 6.25 | 1411 N. Wells St |
| 20 | Mindy’s HotChocolate | Crab Cake | 15 | 1747 N. Damen Ave |
| 21 | Olga’s Delicatessen | Chicken Schnitzel | 5 | 3209 W. Irving Park Rd |
| 22 | Dawali Mediterranean Kitchen | Shawarma | 6 | Multiple location |
| 23 | Big Jones | Toasted Pimiento Cheese | 8 | 5347 N. Clark St |
| 24 | La Pane | Vegetarian Panino | 5.99 | 2954 W. Irving Park Rd |
| 25 | Pastoral | Cali Chèvre | 7.52 | Multiple location |
| 26 | Max’s Deli | Pastrami | 11.95 | 191 Skokie Valley Rd |
| 27 | Lucky’s Sandwich Co. | The Fredo | 7.5 | Multiple location |
| 28 | City Provisions | Smoked Ham | 12.95 | 1818 W. Wilson Ave |
| 29 | Papa’s Cache Sabroso | Jibarito | 7 | 2517 W. Division St |
| 30 | Bavette’s Bar & Boeuf | Shaved Prime Rib | 21 | 218 W. Kinzie St |
| 31 | Hannah’s Bretzel | Serrano Ham and Manchego Cheese | 9.79 | Multiple location |
| 32 | La Fournette | Tuna Salad | 9.75 | 1547 N. Wells St |
| 33 | Paramount Room | Paramount Reuben | 13 | 415 N. Milwaukee Ave |
| 34 | Melt Sandwich Shoppe | The Istanbul | 7.95 | 1840 N. Damen Ave |
| 35 | Floriole Cafe & Bakery | B.A.D. | 9 | 1220 W. Webster Ave |
| 36 | First Slice Pie Café | Duck Confit and Mozzarella | 9 | 5357 N. Ashland Ave |
| 37 | Troquet | Croque Monsieur | 8 | 1834 W. Montrose Ave |
| 38 | Grahamwich | Green Garbanzo | 8 | 615 N. State St |
| 39 | Saigon Sisters | The Hen House | 7 | Multiple location |
| 40 | Rosalia’s Deli | Tuscan Chicken | 6 | 241 N. York Rd |
| 41 | Z&H MarketCafe | The Marty | 7.25 | 1323 E. 57th St |
| 42 | Market House on the Square | Whitefish | 11 | 655 Forest Ave |
| 43 | Elaine’s Coffee Call | Oat Bread, Pecan Butter, and Fru | 6 | Hotel Lincol |
| 44 | Marion Street Cheese Market | Cauliflower Melt | 9 | 100 S. Marion St |
| 45 | Cafecito | Cubana | 5.49 | 26 E. Congress Pkwy |
| 46 | Chickpea | Kufta | 8 | 2018 W. Chicago Ave |
| 47 | The Goddess and Grocer | Debbie’s Egg Salad | 6.5 | 25 E. Delaware Pl |
| 48 | Zenwich | Beef Curry | 7.5 | 416 N. York St |
| 49 | Toni Patisserie | Le Végétarien | 8.75 | 65 E. Washington St |
| 50 | Phoebe’s Bakery | The Gatsby | 6.85 | 3351 N. Broadwa |
+---------+---------------------------------+----------------------------------+-------+-------------------------+
SELECT UCASE(string);
다음 문장을 모두 대문자로 조회
mysql> select ucase('This is ucase test.');
+------------------------------+
| ucase('This is ucase test.') |
+------------------------------+
| THIS IS UCASE TEST. |
+------------------------------+
$15가 넘는 메뉴를 대문자로 조회
mysql> select ucase(menu) from sandwich where price > 15;
+----------------------------------+
| ucase(menu) |
+----------------------------------+
| LOBSTER ROLL |
| GRILLED LAUGHING BIRD SHRIMP AND |
| SHAVED PRIME RIB |
+----------------------------------+
SELECT LCASE(string);
다음 문장을 모두 대문자로 조회
mysql> select lcase('THIS IS LCASE TEST.');
+------------------------------+
| lcase('THIS IS LCASE TEST.') |
+------------------------------+
| this is lcase test. |
+------------------------------+
$5가 안 되는 메뉴를 소문자로 조회
mysql> select lcase(menu) from sandwich where price < 5;
+--------------+
| lcase(menu) |
+--------------+
| meatball sub |
+--------------+
SELECT MID(string, start_position, length);
1번 위치에서 4글자를 조회
mysql> select mid('This is mid test.', 1, 4);
+--------------------------------+
| mid('This is mid test.', 1, 4) |
+--------------------------------+
| This |
+--------------------------------+
6번 위치에서 5글자를 조회
mysql> select mid('This is mid test.', 6, 5);
+--------------------------------+
| mid('This is mid test.', 6, 5) |
+--------------------------------+
| is mi |
+--------------------------------+
뒤에서 4번째에서 4글자 조회
mysql> select mid('This is mid test.', -4, 4);
+---------------------------------+
| mid('This is mid test.', -4, 4) |
+---------------------------------+
| est. |
+---------------------------------+
뒤에서 8번째에서 3글자 조회
mysql> select mid('This is mid test.', -8, 3);
+---------------------------------+
| mid('This is mid test.', -8, 3) |
+---------------------------------+
| id |
+---------------------------------+
11위 카페이름 중 두번째 단어만 뒤에서 6번째 위치에서 4글자 조회
mysql> select mid(cafe,6,4) from sandwich where ranking='11';
+---------------+
| mid(cafe,6,4) |
+---------------+
| Cafe |
+---------------+
SELECT LENGTH(string);
다음 문장의 길이를 조회
mysql> select length('This is a len test.');
+-------------------------------+
| length('This is a len test.') |
+-------------------------------+
| 19 |
+-------------------------------+
문자가 없는 경우
mysql> select length('');
+------------+
| length('') |
+------------+
| 0 |
+------------+
공백의 경우
mysql> select length(' ');
+-------------+
| length(' ') |
+-------------+
| 1 |
+-------------+
NULL의 경우
mysql> select length(NULL);
+--------------+
| length(NULL) |
+--------------+
| NULL |
+--------------+
sandwich 테이블에서 Top 3의 주소 길이 검색
mysql> select length(address), address from sandwich where ranking <= 3;
+-----------------+---------------------+
| length(address) | address |
+-----------------+---------------------+
| 19 | 2109 W. Chicago Ave |
| 18 | 800 W. Randolph St |
| 16 | 445 N. Clark St |
+-----------------+---------------------+
SELECT ROUND(number, decimals_place);
반올림할 위치를 지정하지 않을 경우, 소수 첫째 자리(0)에서 반올림
mysql> select round(315.625);
+----------------+
| round(315.625) |
+----------------+
| 316 |
+----------------+
첫 번째 소수점 위치
mysql> select round(315.625, 0);
+-------------------+
| round(315.625, 0) |
+-------------------+
| 316 |
+-------------------+
두 번째 소수점 위치
mysql> select round(315.625, 1);
+-------------------+
| round(315.625, 1) |
+-------------------+
| 315.6 |
+-------------------+
세 번째 소수점 위치
mysql> select round(315.625, 2);
+-------------------+
| round(315.625, 2) |
+-------------------+
| 315.63 |
+-------------------+
일단위 위치
mysql> select round(315.625, -1);
+--------------------+
| round(315.625, -1) |
+--------------------+
| 320 |
+--------------------+
십단위 위치
mysql> select round(315.625, -2);
+--------------------+
| round(315.625, -2) |
+--------------------+
| 300 |
+--------------------+
sandwich 테이블에서 소주점 자리는 반올림해서 1달러 단위까지만 최하위 3개 표시
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 |
+---------+-------+--------------+
SELECT NOW();
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2023-02-16 10:04:33 |
+---------------------+
SELECT FORMAT(number, decimal_place);
소수점을 표시하지 않을 경우
mysql> select format(12345.6789, 0);
+-----------------------+
| format(12345.6789, 0) |
+-----------------------+
| 12,346 |
+-----------------------+
소수점 두자리까지 표시할 경우
mysql> select format(12345.6789, 2);
+-----------------------+
| format(12345.6789, 2) |
+-----------------------+
| 12,345.68 |
+-----------------------+
소수점 열자리까지 표시할 경우
mysql> select format(12345.6789, 10);
+------------------------+
| format(12345.6789, 10) |
+------------------------+
| 12,345.6789000000 |
+------------------------+