mysql – 让in查询的结果按in参数列表排序

select * from t where id in (3,1,2) 

结果可能是

1, xxx

2, xxx

3, xxx

或者其它,反正不确定

select * from t where id in (3,1,2)  order by field(t.id 3, 1, 2)

就可以确定得到这样的排序结果

3, xxx

1, xxx

2, xxx

在ibatis里这样写:

 	<select id="someSql" parameterClass="list" resultMap="resultMap">		 
			select *
			from t
			where  id in 
		  <iterate open="(" close=")" conjunction=",">
  			 #[]#
  		  </iterate>										
			order by FIELD(t.id, 			
		  <iterate conjunction=",">
  			 #[]#
  		  </iterate>		
			)		 
	</select>


Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.