• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

三木筒子

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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)
如有错误,请邮件zs253499660@sina.com,如有更好的方法,可以推荐

posted on 2020-06-19 17:23  森酱  阅读(3249)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3