[MyBatis] IN절의 조건 순서 그대로 정렬하는 방법

Published: by Creative Commons Licence

MySQL 혹은 MariaDB

SELECT PRD_ID
FROM   PRODUCT
WHERE PRD_ID IN (<foreach item="item" collection="prdIds" separator=",">#{item}</foreach>)
ORDER BY FIELD(PRD_ID, <foreach item="item" collection="prdIds" separator=",">#{item}</foreach>)

Oracle

SELECT PRD_ID
FROM   PRODUCT
WHERE PRD_ID IN (<foreach item="item" collection="prdIds" separator=",">#{item}</foreach>)
ORDER BY CASE PRD_ID <foreach item="item" collection="prdIds" separator=" " index="index">WHEN #{item} THEN #{index}</foreach> END

끗.