电脑疯子技术论坛|电脑极客社区

 找回密码
 注册

QQ登录

只需一步,快速开始

[编程和数据库] 详解 Mysql查询结果顺序按 in() 中ID 的顺序排列

[复制链接]
zhaorong 发表于 2017-9-26 14:48:22 | 显示全部楼层 |阅读模式

这篇文章主要介绍了详解 Mysql查询结果顺序按 in() 中ID 的顺序排列的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

详解 Mysql查询结果顺序按 in() 中ID 的顺序排列
实例代码:


  1. <select id="queryGBStyleByIDs" resultMap="styleMap">
  2.     select style_num_id ,style_id,style_title,style_pic FROM gb_style where online = 1 AND is_hide = 0 and style_num_id in
  3.     <foreach collection="styleNumIDs" item="styleNumId" separator="," open="(" close=")">
  4.      #{styleNumId}
  5.     </foreach>
  6.     ORDER BY FIELD
  7.     <foreach collection="styleNumIDs" item="styleNumId" separator="," open="(style_num_id," close=")">
  8.       #{styleNumId}
  9.     </foreach>
  10.   </select>
复制代码


最终输出sql如下:

  1. select style_num_id ,style_id,style_title,style_pic FROM gb_style where online =
  2. 1 AND is_hide = 0 and style_num_id in (1,3,2,5)
  3. order by field (style_num_id,1,3,2,5);
复制代码


如有疑问请留言或者到本站社区交流讨论,感谢阅读。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|VIP|电脑疯子技术论坛 ( Computer madman team )

GMT+8, 2025-3-14 04:02

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表