๐ ๋ฐฐ์ด + select + ์กฐํ์กฐ๊ฑด in ์กฐํฉ์ ๊ฐ๋ฅํ๊ฒ ํด์ฃผ๋ ๊ฒ ๋ฐ๋ก foreach !
ํ์ id๋ฅผ ํฌํจํ ํ์ ์ธ์ ์ ๋ณด๊ฐ ๋ด๊ฒจ์๋ ๋ฐฐ์ด์ด ์๋ค๊ณ ํ ๋, ๊ทธ ๋ฐฐ์ด๋ก ํ์์ ์ฑ์ ์ ์กฐํํ๊ณ ์ถ๋ค๊ณ ํด๋ณด์.
์ผ๋จ = ๋ก๋ ์กฐํํ ์ ์๊ณ , in์ ์๊ฐํ๊ฒ ๋๋๋ฐ, in (1,2,3,4,5) ์ด๋ฐ์์ผ๋ก ์์๊ฒ ๋ค์ด๊ฐ๋ฆฌ ๋ง๋ฌดํ๋ค ใ ใ
์๋ํด๋ดค๋๋ฐ in ('1,2,3,4,5') ์ด๋ ๊ฒ ๋ค์ด๊ฐ๋ฒ๋ฆฐ๋ค..
์ํผ ์ด๋ด ๋ ์ฌ์ฉํ ์ ์๋ foreach!
collection : ์ ๋ฌ๋ฐ์ ์ธ์. List or Array ํํ๋ง ๊ฐ๋ฅ
item : ์ ๋ฌ๋ฐ์ ์ธ์ ๊ฐ์ alias ๋ช ์ผ๋ก ๋์ฒด
open : ๊ตฌ๋ฌธ์ด ์์๋ ๋ ์ฝ์ ํ ๋ฌธ์์ด
close : ๊ตฌ๋ฌธ์ด ์ข ๋ฃ๋ ๋ ์ฝ์ ํ ๋ฌธ์์ด
separator : ๋ฐ๋ณต ๋๋ ์ฌ์ด์ ์ถ๋ ฅํ ๋ฌธ์์ด
index : ๋ฐ๋ณต๋๋ ๊ตฌ๋ฌธ ๋ฒํธ. 0๋ถํฐ ์์ฐจ์ ์ผ๋ก ์ฆ๊ฐ
SELECT *
FROM student_score
WHERE student_id in
<foreach collection="studentIdArr" item="item" index="index" separator="," open="(" close=")">
#{item.id}
</foreach>
xml ํ์ผ์๋ ์ด๋ ๊ฒ ์จ์ฃผ๋ฉด ๋๋ค.
() => {
const studentIdArr: any[] = [];
for (let i = 0; i < studentInfoList?.length; i+=1){
studentIdArr.push({
id : studentInfo[i].studentId
});
}
}
ํ๋ก ํธ์๋ (ํ์ ์คํฌ๋ฆฝํธ, ๋ฆฌ์กํธ)์์ studenetIdArr๋ ์ด๋ ๊ฒ ๊ตฌ์ฑํด์ฃผ์๋ค.
์ ๋ ๊ฒ 'id'๋ผ๋ key๊ฐ ์์ด์ผ ๋ค์ด๊ฐ๋๋ผ.. studentInfoList๊ฐ object๋ผ์ ์ ๋ ๊ฒ studentIdArr ๋ฐฐ์ด์ push๋ฅผ ํด์ฃผ์์ง๋ง,
๋ฐฐ์ด์ด๋ผ๋ฉด ๊ตณ์ด for๋ฌธ ๋๋ฆด ํ์ ์๋ค.