[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv1. ์—ฌ๋Ÿฌ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ [SELECT]

2022. 9. 22. 13:15ใ†์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_2022

728x90

๋ฌธ์ œ ์„ค๋ช…

ANIMAL_INS ํ…Œ์ด๋ธ”์€ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ANIMAL_INS ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, SEX_UPON_INTAKE๋Š” ๊ฐ๊ฐ ๋™๋ฌผ์˜ ์•„์ด๋””, ์ƒ๋ฌผ ์ข…, ๋ณดํ˜ธ ์‹œ์ž‘์ผ, ๋ณดํ˜ธ ์‹œ์ž‘ ์‹œ ์ƒํƒœ, ์ด๋ฆ„, ์„ฑ๋ณ„ ๋ฐ ์ค‘์„ฑํ™” ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

 

NAME                                                                                   TYPE                                                      NULLABLE
ANIMAL_ID VARCHAR(N) FALSE
ANIMAL_TYPE VARCHAR(N) FALSE
DATETIME DATETIME FALSE
INTAKE_CONDITION VARCHAR(N) FALSE
NAME VARCHAR(N) TRUE
SEX_UPON_INTAKE VARCHAR(N) FALSE

๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋ชจ๋“  ๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„, ๋ณดํ˜ธ ์‹œ์ž‘์ผ์„ ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

๋‹จ, ์ด๋ฆ„์ด ๊ฐ™์€ ๋™๋ฌผ ์ค‘์—์„œ๋Š” ๋ณดํ˜ธ๋ฅผ ๋‚˜์ค‘์— ์‹œ์ž‘ํ•œ ๋™๋ฌผ์„ ๋จผ์ € ๋ณด์—ฌ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ

์˜ˆ๋ฅผ ๋“ค์–ด, ANIMAL_INS ํ…Œ์ด๋ธ”์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด

 

ANIMAL_ID          ANIMAL_TYPE      DATETIME                      INTAKE_CONDITION   NAME   SEX_UPON_INTAKE
A349996 Cat 2018-01-22 14:32:00 Normal Sugar Neutered Male
A350276 Cat 2017-08-13 13:50:00 Normal Jewel Spayed Female
A396810 Dog 2016-08-22 16:13:00 Injured Raven Spayed Female
A410668 Cat 2015-11-19 13:41:00 Normal Raven Spayed Female
  1. ์ด๋ฆ„์„ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, 'Jewel', 'Raven', 'Sugar'
  2. 'Raven'์ด๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ๊ฐœ์™€ ๊ณ ์–‘์ด๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ, ์ด ์ค‘์—์„œ๋Š” ๋ณดํ˜ธ๋ฅผ ๋‚˜์ค‘์— ์‹œ์ž‘ํ•œ ๊ฐœ๋ฅผ ๋จผ์ € ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ SQL๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

ANIMAL_ID                                NAME                             DATETIME
A350276 Jewel 2017-08-13 13:50:00
A396810 Raven 2016-08-22 16:13:00
A410668 Raven 2015-11-19 13:41:00
A349996 Sugar 2018-01-22 14:32:00
 

 

 

 

๋‚˜์˜ ํ’€์ด [MySQL] 

SELECT ANIMAL_ID, NAME, DATETIME FROM ANIMAL_INS ORDER BY NAME, DATETIME DESC;

-> ๋จผ์ € ์กฐํšŒ๋ฅผ ํ•˜๋Š”๋ฐ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์•„์ด๋””, ์ด๋ฆ„, ๋ณดํ˜ธ ์‹œ์ž‘์ผ์„ ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์กฐํšŒ๋ฅผ ํ•˜๊ณ , 

์ด๋•Œ ์ด๋ฆ„์ด ๊ฐ™์€ ๋™๋ฌผ์ด ์žˆ์œผ๋ฉด ๋ณดํ˜ธ๋ฅผ ๋‚˜์ค‘์— ์‹œ์ž‘ํ•œ ๋™๋ฌผ์„ ๋จผ์ € ๋ณด์—ฌ์ค˜์•ผ ํ•œ๋‹ค๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ

ORDER BY NAME, DATETIME DESC๋ฅผ ํ†ตํ•ด ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๊ณ , ๊ทธ ๋‹ค์Œ DATETIME DESC ๋กœ ๋‚˜์ค‘์— ์‹œ์ž‘ํ•œ ๋™๋ฌผ์„ ๋ณด์—ฌ์คฌ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90