๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋ชฉ๋ก์ด ์—†์Šต๋‹ˆ๋‹ค.

MySQL IN ์ ˆ ์•ˆ์— ์žˆ๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ

๐Ÿ›ข DB/MySQL

    SQL ์ฟผ๋ฆฌ๋ฅผ ์งœ๋‹ค๋ณด๋ฉด ์ •๋ ฌ์„ ๋‚ด ๋ง˜๋Œ€๋กœ ํ•˜๊ณ ์‹ถ์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ ๋‚˜๋Š” ์ฟผ๋ฆฌ๋Š” ์ง„์งœ ๋ถ€์กฑํ•œ ํŽธ์ด๋ผ ๊ตฌ๊ธ€๋ง์„ ๋งŽ์ด ํ•˜๋Š” ํŽธ์ด์—์š”. ์ด๋ฒˆ์—๋Š” ๊ตฌ๊ธ€๋งํ•˜๋‹ค๊ฐ€ ์ด๋ ‡๊ฒŒ๋„ ๋˜๋Š”๊ตฌ๋‚˜ ๊นจ๋‹ฌ์•„์„œ ์ •๋ฆฌํ•ด๋†“์œผ๋ ค๊ณ  ํ•ด์š”. ์ €๋Š” MySQL์ด๋‚˜ MariaDB๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๋‘˜์˜ ์ฐจ์ด๋Š” ํ˜„์žฌ๋กœ์„  ๊ฑฐ์˜ ์—†๋‹ค๊ณ  ๋ณด๋ฉด ๋ผ์š”. 

    ์ƒํ™ฉ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ IN ๊ตฌ๋ฌธ ์•ˆ์— PRIMARY KEY ๊ฐ’์„ ๋‚˜์—ดํ•˜์—ฌ ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ํ•„ํ„ฐ๋งํ•˜๋Š” ์‹์ด์—์š”. ๊ทธ๋Ÿฐ๋ฐ ๊ฐ€์ ธ์™€๋†“๊ณ  ๋ณด๋‹ˆ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ •๋ ฌ ์ˆœ์„œ๋Š” IN ๊ตฌ๋ฌธ ์•ˆ์˜ PRIMARY KEY์˜ ์ˆœ์„œ์ธ๋ฐ ์ด๊ฒŒ ์ž๋™์œผ๋กœ PRIMARY KEY๊ฐ’์œผ๋กœ ์ •๋ ฌ์ด ๋˜์–ด ๋‚˜์˜ค๋Š”๊ฒƒ ๋•Œ๋ฌธ์— View ์˜์—ญ์— ์˜ˆ์ƒ๊ณผ๋Š” ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฌผ์ด ๋‚˜์™”์–ด์š”.

     


     

    ์ƒํ™ฉ

    SELECT * FROM TABLE WHERE KEY IN (2, 1, 3)

     

    ํ•ด๊ฒฐ๋ฐฉ๋ฒ• (MySQL IN ์ ˆ ์•ˆ์— ์žˆ๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ)

    ORDER BY FIELD(KEY, 1, 2, 3)

     

    ์œ„์™€ ๊ฐ™์ด ORDER BY ๊ตฌ๋ฌธ์—  FIELD๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ผ์š”. ๋ฌธ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ์ƒ์ž๋งŒ ๋ด๋„ ์•Œ ์ˆ˜ ์žˆ์„๊ฑฐ์—์š”.

     

    SELECT * FROM TABLE WHERE KEY IN (2, 1, 3) ORDER BY FIELD(KEY, 1, 2, 3)

     

    ์œ„์— ์ฟผ๋ฆฌ๊ฐ€ ์ œ๊ฐ€ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฌผ์„ ๋ฝ‘์•„์คฌ์–ด์š” :)

     


     

    ์ €์ฒ˜๋Ÿผ ํ—ค๋งค์‹œ๋Š” ๋ถ„๋“ค์—๊ฒŒ ์กฐ๊ธˆ์ด๋ผ๋„ ๋„์›€์ด ๋˜์—ˆ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค :)