์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(1) (SQL/JOIN)

PROGRAMMING/Database

by koharin 2021. 1. 30. 11:36

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ป SQL ์ œ์ถœ์ฝ”๋“œ


SELECT A.NAME, A.DATETIME // ANIMAL_INS์— ์žˆ๋Š” NAME, DATETIME ์กฐํšŒ
FROM ANIMAL_INS A
LEFT OUTER JOIN ANIMAL_OUTS B
ON A.ANIMAL_ID = B.ANIMAL_ID
WHERE B.ANIMAL_ID IS NULL // ANIMAL_INS์—๋งŒ ์žˆ๋Š” ANIMAL_ID
ORDER BY A.DATETIME ASC // ๋ณดํ˜ธ ์‹œ์ž‘์ผ ์ˆœ ์ •๋ ฌ
LIMIT 3; // ๊ฐ€์žฅ ์˜ค๋ž˜ ์žˆ์—ˆ๋˜ 3๋งˆ๋ฆฌ๋กœ ์ œํ•œ

 

 

๐Ÿ“ ๋ฌธ์ œํ’€์ด ๊ณผ์ •


  • ์ž…์–‘ ๋ชป ๊ฐ„ ๋™๋ฌผ: ANIMAL_ID๊ฐ€ ANIMAL_OUTS์—๋Š” ์—†๋Š”๋ฐ ANIAML_INS์—๋Š” ์žˆ๋Š”(WHERE B.ANIMAL_ID IS NULL) LEFT OUTER JOIN์— ํ•ด๋‹นํ•œ๋‹ค.

  • ๋ณดํ˜ธ ์‹œ์ž‘์ผ ์ˆœ ์ •๋ ฌ: ORDER BY A.DATETIME ASC

  • ๊ฐ€์žฅ ์˜ค๋ž˜ ์žˆ์—ˆ๋˜ 3๋งˆ๋ฆฌ: ORDER BY๋กœ ์ •๋ ฌ๋œ ๊ฒƒ์—์„œ LIMIT 3๋ฅผ ํ•˜๋ฉด ๊ฐ€์žฅ ์˜ค๋ž˜ ์žˆ์—ˆ๋˜ 3๋งˆ๋ฆฌ๋งŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ