728x90

DB(Database)/MySQL 8

Hackerrank SQL Occupations

https://www.hackerrank.com/challenges/occupations/problem?isFullScreen=true Occupations | HackerRank Pivot the Occupation column so the Name of each person in OCCUPATIONS is displayed underneath their respective Occupation. www.hackerrank.com -- pivot table ์ด์šฉ -- ๋ฌธ์ž์—ด์—์„œ MIN์€ a๋ถ€ํ„ฐ ์ถ”์ถœ -- ๋ณ€์ˆ˜ ์„ค์ • set @ -- sub table ์ด๋ฆ„ ์ง€์ • SET @D=0, @P=0, @S=0, @A=0; SELECT MIN(Doctor), MIN(Professor), MIN(Singer), MIN(A..

DB(Database)/MySQL 2021.11.04

[MySQL] ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„ ํ…Œ์ด๋ธ” SQL ์ฟผ๋ฆฌ๋ฌธ์œผ๋กœ JOIN ํ•˜๊ธฐ

์ผ๋Œ€๋‹ค, ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„ 1:๋‹ค ๊ด€๊ณ„ ํ•œ์ชฝ ํ…Œ์ด๋ธ”์—๋Š” ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์กด์žฌํ•˜๊ณ , ๊ทธ ๊ฐ’๋งŒ ๋Œ€์‘๋˜๋Š” ๋‹ค๋ฅธ ์ชฝ ํ…Œ์ด๋ธ”์˜ ๊ฐ’์€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ด€๊ณ„ EX) ํ•™์ƒ ํ…Œ์ด๋ธ”๊ณผ ์ ์ˆ˜ ํ…Œ์ด๋ธ”, ์ง์› ํ…Œ์ด๋ธ”๊ณผ ์›”๊ธ‰ ํ…Œ์ด๋ธ” ๋‹ค:๋‹ค ๊ด€๊ณ„ ํ•œ ํ•™์ƒ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋™์•„๋ฆฌ์— ๊ฐ€์ž…ํ•  ์ˆ˜ ์žˆ๊ณ , ํ•œ ๋™์•„๋ฆฌ์—๋Š” ์—ฌ๋Ÿฌ ํ•™์ƒ์ด ๊ฐ€์ž…ํ•  ์ˆ˜ ์žˆ์–ด ํ•™์ƒ ํ…Œ์ด๋ธ”๊ณผ ๋™์•„๋ฆฌ ํ…Œ์ด๋ธ”์€ ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„์ž„. ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„์˜ ๊ฒฝ์šฐ ์—ฐ๊ฒฐ ํ…Œ์ด๋ธ”๊ณผ ๋‘ ํ…Œ์ด๋ธ”์ด ์ผ๋Œ€๋‹ค ๊ด€๊ณ„๋ฅผ ๋งบ๋„๋ก ๊ตฌ์„ฑ EX) ํ•™์ƒ ํ…Œ์ด๋ธ”, ํ•™์ƒ_๋™์•„๋ฆฌ ํ…Œ์ด๋ธ”(ํ•™์ƒํ…Œ์ด๋ธ”๊ณผ ๋™์•„๋ฆฌํ…Œ์ด๋ธ”์˜ ์—ฐ๊ฒฐ ํ…Œ์ด๋ธ”), ๋™์•„๋ฆฌ ํ…Œ์ด๋ธ” ํ•™์ƒ ํ…Œ์ด๋ธ”, ํ•™์ƒ_๋™์•„๋ฆฌ ํ…Œ์ด๋ธ”, ๋™์•„๋ฆฌ ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•˜๋Š” ์ฟผ๋ฆฌ๋ฌธ -- ํ•™์ƒ ํ…Œ์ด๋ธ” drop table if exists stdtbl; create table stdtbl( stdname varch..

DB(Database)/MySQL 2021.02.16

[MySQL] ๋Œ€์šฉ๋Ÿ‰ ํ…Œ์ด๋ธ” csv, txt ํŒŒ์ผ ํ˜•ํƒœ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ & ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

txt ํŒŒ์ผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ select * from [ํ…Œ์ด๋ธ”๋ช…] into outfile '[ํŒŒ์ผ๋ช…].txt' character set utf8mb4 fields terminated by ',', optionally enclosed by '"' escaped by '\\' lines terminated by '\n'; csv ํŒŒ์ผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ select * from [ํ…Œ์ด๋ธ”๋ช…] into outfile '[ํŒŒ์ผ๋ช…].csv' character set utf8mb4 fields terminated by ',', optionally enclosed by '"' escaped by '\\' lines terminated by '\n'; txt ํŒŒ์ผ ํ…Œ์ด๋ธ”๋กœ ์ฝ์–ด์˜ค๊ธฐ load data infile '[ํŒŒ์ผ๋ช…].txt' ..

DB(Database)/MySQL 2021.02.09

[MySQL] ๋‚ด์žฅํ•จ์ˆ˜ - ์ˆ˜ํ•™ ํ•จ์ˆ˜, ๋‚ ์งœ/์‹œ๊ฐ„ ํ•จ์ˆ˜, ์‹œ์Šคํ…œ/์ •๋ณด ํ•จ์ˆ˜

์ˆ˜ํ•™ ํ•จ์ˆ˜ ABS(์ˆซ์ž) : ์ ˆ๋Œ€๊ฐ’ ๊ณ„์‚ฐ CEILING(์ˆซ์ž) : ์˜ฌ๋ฆผ FLOOR(์ˆซ์ž) : ๋‚ด๋ฆผ ROUNG(์ˆซ์ž) : ๋ฐ˜์˜ฌ๋ฆผ CONV(์ˆซ์ž, ๊ธฐ์กด ์ง„์ˆ˜, ๋ฐ”๊ฟ€ ์ง„์ˆ˜) : ๊ธฐ์กด ์ง„์ˆ˜์—์„œ ๋‹ค๋ฅธ ์ง„์ˆ˜๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ•จ์ˆ˜ SELECT ABS(-100); SELECT CEILING(4.7), FLOOR(4.7), ROUND(4.7); SELECT CONV('AA',16,2), CONV(100,10,8); -- 16์ง„์ˆ˜ AA๋ฅผ 2์ง„์ˆ˜๋กœ ๋ณ€๊ฒฝ, 10์ง„์ˆ˜์˜ 100์„ 8์ง„์ˆ˜๋กœ ๋ณ€๊ฒฝ MOD(์ˆซ์ž1, ์ˆซ์ž2), ์ˆซ์ž1 % ์ˆซ์ž2 : ์ˆซ์ž1์„ ์ˆซ์ž2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ ๋ฐ˜ํ™˜ POW(์ˆซ์ž1, ์ˆซ์ž2) : ์ˆซ์ž1์„ ์ˆซ์ž2๋งŒํผ ๊ฑฐ๋“ญ์ œ๊ณฑํ•œ ๊ฐ’ ๋ฐ˜ํ™˜ SQRT(์ˆซ์ž) : ์ˆซ์ž์˜ ์ œ๊ณฑ๊ทผ ๋ฐ˜ํ™˜ select mod(228, 10), 228%10, ..

DB(Database)/MySQL 2021.02.06

[MySQL] ๋‚ด์žฅํ•จ์ˆ˜ - ๋ฌธ์ž์—ด ํ•จ์ˆ˜

ASCII(์•„์Šคํ‚ค์ฝ”๋“œ), CHAR(์ˆซ์ž) SELECT ASCII('A'), CHAR(65); --> 65, A๋ฐ˜ํ™˜ BIT_LENGTH, CHAR_LENGHT, LENGTH BIT_LENGTH(): ํ• ๋‹น๋œ ๋น„ํŠธํฌ๊ธฐ ๋ฐ˜ํ™˜ CHAR_LENGTH(): ๋ฌธ์ž ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜ LENGTH(): ํ• ๋‹น๋œ ๋ฐ”์ดํŠธ ์ˆ˜ ๋ฐ˜ํ™˜ ์˜๋ฌธ: ๋ฌธ์ž๋‹น 1๋ฐ”์ดํŠธ, ํ•œ๊ธ€: ๋ฌธ์ž๋‹น 3๋ฐ”์ดํŠธ SELECT BIT_LENGTH('ABC'), CHAR_LENGTH('ABC), LENGTH('ABC); --24, 3, 3๋ฐ˜ํ™˜ SELECT BIT_LENGTH('๊ฐ€๋‚˜๋‹ค'), CHAR_LENGTH('๊ฐ€๋‚˜๋‹ค'), LENGTH('๊ฐ€๋‚˜๋‹ค'); -- 72, 3, 9 ๋ฐ˜ํ™˜ CONCAT(๋ฌธ์ž์—ด1, ๋ฌธ์ž์—ด2, ...), CONCAT_WS(๋ฌธ์ž์—ด1, ๋ฌธ์ž์—ด2, ...) CO..

DB(Database)/MySQL 2021.01.20

[MySQL] MySQL ๋ณ€์ˆ˜ ์‚ฌ์šฉ๋ฐฉ๋ฒ•(SET, PREPARE, EXECUTE)๊ณผ ๋ฐ์ดํ„ฐ ํ˜•์‹ ๋ณ€ํ™˜ ํ•จ์ˆ˜(CAST, CONVERT,CONCAT), ๋‚ด์žฅํ•จ์ˆ˜(์ œ์–ดํ•จ์ˆ˜ IF, IFNULL, NULLIF, CASE WHEN ELSE END)

๋ณ€์ˆ˜ ์‚ฌ์šฉ ํ˜•์‹ SET @๋ณ€์ˆ˜๋ช… = ๋ณ€์ˆ˜๊ฐ’; -- ๋ณ€์ˆ˜ ์„ ์–ธ, ๊ฐ’ ๋Œ€์ž… SELECT @๋ณ€์ˆ˜์ด๋ฆ„; -- ๋ณ€์ˆ˜ ์ถœ๋ ฅ ์˜ˆ์ œ SET @VAR1 = 1; SET @VAR2 = 2; SELECT @VAR1; -- ๊ฒฐ๊ณผ 1 SELECT @VAR1 + @VAR2; -- ๊ฒฐ๊ณผ 2 SET, PREPARE, EXECUTE ๋ฌธ SET @VAR1 = 1; PREPARE Query FROM 'SELECT * FROM [ํ…Œ์ด๋ธ”๋ช…] ORDER BY [์ •๋ ฌ๊ธฐ์ค€] LIMIT ?'; -- '?'์œ„์น˜์— ๋ณ€์ˆ˜ ์‚ฌ์šฉํ•ด ์ฟผ๋ฆฌ๋ฌธ PREPARE EXECUTE Query USING @VAR1; -- ๋ณ€์ˆ˜ ์‹คํ–‰๋ฌธ ๋ฐ์ดํ„ฐ ํ˜•์‹ ๋ณ€ํ™˜ ํ•จ์ˆ˜ CONVERT(), CAST(): ๊ฐ•์ œ๋กœ ๋ฐ์ดํ„ฐํ˜•์„ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ -- ๋ฐ์ดํ„ฐ๋ฅผ ์ •์ˆ˜๋กœ ์ถœ๋ ฅ SELECT CAST([..

DB(Database)/MySQL 2021.01.20

[MySQL] SQL ์ฟผ๋ฆฌ๋ฌธ ๊ธฐ์ดˆ ๋‹ค์ง€๊ธฐ 1 - MySQL Workbench, SELECT๋ฌธ, USE๊ตฌ๋ฌธ, DB ๋ชฉ๋ก ํ™•์ธ

SELECT๋ฌธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด ํ…Œ์ด๋ธ”์—์„œ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ธฐ์ดˆ์ ์ธ ๊ตฌ๋ฌธ SELECT ์—ด์ด๋ฆ„ FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๋ฝ‘๋Š” ๊ตฌ๋ฌธ ์‚ฌ์šฉ ์‹œ WHERE, GROUP BY, HAVING, ORDER BY์˜ ์ˆœ์„œ๊ฐ€ ์ค‘์š” SELECT ์—ด์ด๋ฆ„ FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ WHERE ์กฐ๊ฑด GROUP BY ๊ธฐ์ค€ HAVING ์กฐ๊ฑด ORDER BY USE๊ตฌ๋ฌธ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ง€์ • Workbench์—์„œ ์ง์ ‘ ์„ ํƒํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ USE [๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…] SELECT * FROM [ํ…Œ์ด๋ธ”๋ช…] ํ…Œ์ด๋ธ” ์•ˆ์˜ ๋ชจ๋“  ์—ด ์ถ”์ถœํ•˜๋Š” ์ฟผ๋ฆฌ๋ฌธ ์„ ํƒํ•œ DB๊ฐ€ sqldb๋ผ๋ฉด ์•„๋ž˜ ๋‘ ๋ฌธ์žฅ์€ ๋™์ผํ•˜๋‹ค SELECT * FROM sqldb.table1; SELECT * FROM table1; MySQL Workbench ..

DB(Database)/MySQL 2021.01.15
728x90