SELECT @rowno:=@rowno+1 as rowno,r.* from grade_table r ,(select @rowno:=0) t where .username like '%a%'
select @rownum:=@rownum+1 as rownum,a.roleId
from base_userroles a,(select @rownum:=0) t
where a.roleId='admin';
mysql按照in里面的顺序来排序:
select * from table where id IN (3,9,6) order by field(id,3,9,6);
出来的顺序就是指定的(3,6,9)顺序了
关于这种排序的效率,
有文章指出:
FIELD(str,str1,str2,str3,…)
Returns the index (position) of str in the str1, str2, str3, … list. Returns 0 if str is not found.
排序过程:把选出的记录的 id 在 FIELD 列表中进行查找,并返回位置,以位置作为排序依据。
这样的用法,会导致 Using filesort,是效率很低的排序方式。除非数据变化频率很低,或者有长时间的缓存,否则不建议用这样的方式排序。
使用explain测试会导致using filesort。
建议在程序代码中自行排序。
select @rowno:=@rowno+1 as rowno, id,nickanme from admin as u ,(select @rowno:=0) t
where u.id in (
11,22
)
order by field(id,11,22)
分析,有可能是被阻塞锁定,可以查看processlist,然后kill掉异常id即可
show full processlist;
kill processid
如下图:
4
http://www.t086.com/article/4818
http://blog.itpub.net/12679300/viewspace-1420009/