group_concat 的坑(需要排序)


代碼中的group_concat如果需要按傳入的list進行排序返回的話,

語句應該如下

SELECT
group_concat(CASE when t.FCUSTOMIZE > 0 then CONCAT(t.fnamepath,'( 自定義)') ELSE t.FNAMEPATH END order by field (fid, <foreach collection="list" separator="," item="item" close=")" index="index">
#{item}
</foreach> SEPARATOR ', ' )
FROM
tknowledgepoint t
WHERE
t.fid in <foreach collection="list" open="(" index="index" close=")" item="item" separator=",">
#{item}
</foreach>
order by field(fid, <foreach collection="list" separator="," item="item" close=")" index="index">
#{item}
</foreach>

concat中的語句/條件請自行替換
主要原因
mysql 的in 查詢 返回無序,按照表中的全表查詢,group_concat返回又是無序,於是如果要按照傳入參數去返回,那么就要加上order by field(id,1,2,3,4)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM