mybatis问题,关于批量的问题

RockeyCui 2016-08-05 09:49:37
随机生成一套试卷,现在要做一个查看历史记录的功能,就是根据试卷上的id查出来,现在的问题是用 mysql的 in 查询后它会把试卷按id大小来排,而不是按试卷上的id顺序。我加了order by find_in_set,却不知道怎么写了,老是出错,求解决!!!
<select id="queryQuestionsByIds" resultMap="BaseResultMap">
SELECT
*
FROM question_tbl
where id in
<foreach collection="ids" item="id" open="(" close=")"
separator=",">
#{id}
</foreach>
<!-- order by find_in_set (id,
<foreach collection="s_ids" item="s_id" open="'"
close="'" separator=",">
#{s_id}
</foreach>
) --> 注释的地方有错误
</select>

List<Question> queryQuestionsByIds(Map<String, List<String>> m_ids);

报错信息如下:
### SQL: SELECT   *   FROM question_tbl   where id in    (      ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    ,     ?    )     order by find_in_set (id,     '       ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     ,      ?     '     )
### Cause: java.sql.SQLException: Parameter index out of range (19 > number of parameters, which is 18).
; SQL []; Parameter index out of range (19 > number of parameters, which is 18).; nested exception is java.sql.SQLException: Parameter index out of range (19 > number of parameters, which is 18).] with root cause
java.sql.SQLException: Parameter index out of range (19 > number of parameters, which is 18).
...全文
205 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Intboy 2016-08-05
  • 打赏
  • 举报
回复
SELECT * FROM question_tbl where id in ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) order by find_in_set (id, ' ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ' ) 这sql有问题吧?后面参数为什么有单引号?
RockeyCui 2016-08-05
  • 打赏
  • 举报
回复
引用 1 楼 qq_35261789 的回复:
SELECT * FROM question_tbl where id in (1,4,2,3) order by field(id,1,4,2,3); 这样就行了
问题是 我把1,4,2,3 作为ids传进去,无法赋值,就会报错了,我的第一套试卷有18道题,看它报错内容好像遍历到“第19道“了,这是什么情况呢?
阳光越来越暖 2016-08-05
  • 打赏
  • 举报
回复
SELECT * FROM question_tbl where id in (1,4,2,3) order by field(id,1,4,2,3); 这样就行了
RockeyCui 2016-08-05
  • 打赏
  • 举报
回复
引用 3 楼 fengspg 的回复:
SELECT * FROM question_tbl where id in ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) order by find_in_set (id, ' ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ' ) 这sql有问题吧?后面参数为什么有单引号?
mysql的find_in_set 就是这么写的吧,不过用二楼的方法解决了。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧