๐ŸŒตํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค SQL ๊ณ ๋“์  Kit [String, Date]

์œค๊ฑดํฌ(Guny)ยท2020๋…„ 10์›” 15์ผ
0

Database

๋ชฉ๋ก ๋ณด๊ธฐ
7/7
post-thumbnail

SQL ๊ณ ๋“์  Kit - String, Date

๐Ÿ“š์ˆซ์ž๋Š” ์–ด๋–ป๊ฒŒ ์“ฐ๋Š” ๊ฑด์ง€ ์•Œ๊ฒ ๋Š”๋ฐ, ๊ธ€์ž์™€ ๋‚ ์งœ๋Š” ์–ด๋–ป๊ฒŒ ๋‹ค๋ฃจ์ง€?


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

๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ค‘ ์ด๋ฆ„์ด Lucy, Ella, Pickle, Rogan, Sabrina, Mitty์ธ ๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„, ์„ฑ๋ณ„ ๋ฐ ์ค‘์„ฑํ™” ์—ฌ๋ถ€๋ฅผ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

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


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

๋ณดํ˜ธ์†Œ์— ๋Œ์•„๊ฐ€์‹  ํ• ๋จธ๋‹ˆ๊ฐ€ ๊ธฐ๋ฅด๋˜ ๊ฐœ๋ฅผ ์ฐพ๋Š” ์‚ฌ๋žŒ์ด ์ฐพ์•„์™”์Šต๋‹ˆ๋‹ค. ์ด ์‚ฌ๋žŒ์ด ๋งํ•˜๊ธธ ํ• ๋จธ๋‹ˆ๊ฐ€ ๊ธฐ๋ฅด๋˜ ๊ฐœ๋Š” ์ด๋ฆ„์— 'el'์ด ๋“ค์–ด๊ฐ„๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ด๋ฆ„ ์ค‘, ์ด๋ฆ„์— "EL"์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฐœ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„์„ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์กฐํšŒํ•ด์ฃผ์„ธ์š”. ๋‹จ, ์ด๋ฆ„์˜ ๋Œ€์†Œ๋ฌธ์ž๋Š” ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

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


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

๋ณดํ˜ธ์†Œ์˜ ๋™๋ฌผ์ด ์ค‘์„ฑํ™”๋˜์—ˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ํŒŒ์•…ํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ค‘์„ฑํ™”๋œ ๋™๋ฌผ์€ SEX_UPON_INTAKE ์ปฌ๋Ÿผ์— 'Neutered' ๋˜๋Š” 'Spayed'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„, ์ค‘์„ฑํ™” ์—ฌ๋ถ€๋ฅผ ์•„์ด๋”” ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ์ค‘์„ฑํ™”๊ฐ€ ๋˜์–ด์žˆ๋‹ค๋ฉด 'O', ์•„๋‹ˆ๋ผ๋ฉด 'X'๋ผ๊ณ  ํ‘œ์‹œํ•ด์ฃผ์„ธ์š”.

SELECT ANIMAL_ID, NAME,
CASE WHEN SEX_UPON_INTAKE LIKE "Neutered%" OR SEX_UPON_INTAKE LIKE "Spayed%"
THEN "O"
ELSE "X"
END AS "์ค‘์„ฑํ™”"
FROM ANIMAL_INS
ORDER BY ANIMAL_ID


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

์ž…์–‘์„ ๊ฐ„ ๋™๋ฌผ ์ค‘, ๋ณดํ˜ธ ๊ธฐ๊ฐ„์ด ๊ฐ€์žฅ ๊ธธ์—ˆ๋˜ ๋™๋ฌผ ๋‘ ๋งˆ๋ฆฌ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„์„ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ๋ณดํ˜ธ ๊ธฐ๊ฐ„์ด ๊ธด ์ˆœ์œผ๋กœ ์กฐํšŒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

SELECT ANIMAL_INS.ANIMAL_ID, ANIMAL_INS.NAME
FROM ANIMAL_INS, ANIMAL_OUTS
WHERE ANIMAL_INS.ANIMAL_ID = ANIMAL_OUTS.ANIMAL_ID
ORDER BY ANIMAL_OUTS.DATETIME - ANIMAL_INS.DATETIME DESC
LIMIT 2


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

ANIMAL_INS ํ…Œ์ด๋ธ”์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ์— ๋Œ€ํ•ด, ๊ฐ ๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„, ๋“ค์–ด์˜จ ๋‚ ์งœ๋ฅผ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ์•„์ด๋”” ์ˆœ์œผ๋กœ ์กฐํšŒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

SELECT ANIMAL_ID, NAME, DATE_FORMAT(DATETIME, "%Y-%m-%d")as "๋‚ ์งœ"
FROM ANIMAL_INS
ORDER BY ANIMAL_ID

  • ์ฃผ์˜ : DATE_FORMAT ๋Œ€์†Œ๋ฌธ์ž ์ฃผ์˜!
    • %y : 18
    • %Y : 2018

profile
๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€