ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๋ฌธ์ œ ํ’€์ด

 ๋ฌธ์ œ์—์„œ๋Š” ํŠน์ • ์กฐ๊ฑด์ด ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’๋“ค๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋‚˜์˜จ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํ•„๋“œ ๊ฐ’์„ ํ™•์ธํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ํ•„๋“œ์˜ ๊ฐ’๋“ค์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” `IN`, `LIKE`์™€ `REGEXP`๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

๋ฃจ์‹œ์™€ ์—˜๋ผ ์ฐพ๊ธฐ

SELECT ANIMAL_ID, NAME, SEX_UPON_INTAKE
FROM ANIMAL_INS
WHERE NAME IN ('Lucy', 'Ella', 'Pickle', 'Rogan', 'Sabrina', 'Mitty')

 `Python`์˜ `in` ์—ฐ์‚ฐ๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” `IN`์„ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฆ„์— el์ด ๋“ค์–ด๊ฐ€๋Š” ๋™๋ฌผ ์ฐพ๊ธฐ

SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE NAME LIKE '%EL%' AND ANIMAL_TYPE = 'Dog'
ORDER BY NAME

 `%EL%`๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, EL์„ ์•ž ๋’ค๋กœ ์–ด๋–ค ๋ฌธ์ž๊ฐ€ ์™€๋„ ๋˜๋ฏ€๋กœ, el์ด ๋“ค์–ด๊ฐ€๋Š” ๋™๋ฌผ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

์ค‘์„ฑํ™” ์—ฌ๋ถ€ ํŒŒ์•…ํ•˜๊ธฐ

SELECT 
    ANIMAL_ID, NAME,
    IF (SEX_UPON_INTAKE REGEXP 'Neutered|Spayed', 'O', 'X') AS ์ค‘์„ฑํ™”
FROM ANIMAL_INS

 ์ค‘์„ฑํ™”๊ฐ€ ๋˜๋ฉด, ๋‘ ๊ฐ€์ง€๋กœ ํ•„๋“œ ๊ฐ’์ด ๋‹ฌ๋ผ์ง€๊ฒŒ ๋˜๋ฏ€๋กœ `REGEXP`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์„ ๊ฒ€์ƒ‰ํ•˜๋ฉด ์›ํ•˜๋Š” ๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(2)

SELECT OUTS.ANIMAL_ID, OUTS.NAME
FROM ANIMAL_INS INS
RIGHT JOIN ANIMAL_OUTS OUTS
ON OUTS.ANIMAL_ID = INS.ANIMAL_ID
WHERE INS.ANIMAL_ID IS NOT NULL
ORDER BY INS.DATETIME - OUTS.DATETIME
LIMIT 2

 ์•ž์„œ ๋‹ค๋ฃฌ ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(1)๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ, ์ž…์–‘์„ ๊ฐ„ ๋™๋ฌผ ์ค‘ ๊ฐ€์žฅ ๋ณดํ˜ธ๊ธฐ๊ฐ„์ด ๊ธด ๋™๋ฌผ์ด๋ผ๋Š” ์กฐ๊ฑด์ด ๋‹ค๋ฅด๋‹ค. ์ด๋ฅผ ์œ„ํ•ด `ORDER BY INS.DATETIME - OUTS.DATETIME`์„ ํ†ตํ•ด ์ •๋ ฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

DATETIME์—์„œ DATE๋กœ ํ˜• ๋ณ€ํ™˜

SELECT ANIMAL_ID, NAME, DATE_FORMAT(DATETIME, '%Y-%m-%d') AS ๋‚ ์งœ
FROM ANIMAL_INS

 `DATA_FORMAT`์„ ํ†ตํ•ด ํ•„๋“œ์˜ ์‹œ๊ฐ„ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. (%Y, %y, %M, %m, %D, %d)๋Š” ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€