mysql id in 排列问题
2012-06-04 10:30
344 查看
mysql id in 排列问题
SELECT * FROM szmg0112.cms_archives c where id in(13934,13914,13915,13923,13910,13891,13920,13882,13937,13924,13881) order by substring_index('13934,13914,13915,13923,13910,13891,13920,13882,13937,13924,13881',id,1) 语句可以根据id里面的顺序来排列查询。 select id from table where id in (2,1,3,5) order by field(id,2,1,3,5) mysql in 默认的排序 select id from table where id in (2,1,3,5); 查出来的结果是:1 2 3 5 但是有的时候是要:2 1 3 5 这就需要做排序的时候做处理,网上有2种方法,但是没有详细的解释。查了下手册,记下方便自己以后查找。 select id from table where id in (2,1,3,5) order by substring_index('2,1,3,5',id,1); substring_index(str,delim,count) 字符串截取函数 str 要截取的字符串 delim 截取的分割符 count 截取的数量 返回从字符串str的第count个出现的分隔符delim之后的子串。如果count是正数,返回最后的分隔符到左边(从左边数) 的所有字符。如果count是负数,返回最后的分隔符到右边的所有字符(从右边数)。 那 order by substring_index(‘2,1,3,5′,id,1) 这个啥意思呢 这个是在字符串’2,1,3,5′里查找id,如果找不到,就返回整个字符串 select id from table where id in (2,1,3,5) order by find_inset(id,'2,1,3,5') find_in_set(str,strlist) 字符串函数 如果字符串str在由N子串组成的表strlist之中,返回一个1到N的值。如果str不是在strlist里面或如果strlist是空字符串,返回0。 SELECT FIND_IN_SET('b','a,b,c,d'); -> 2 所以 find_in_set返回的是一个1到N的值,在order by的话,就是按顺序排了。 |
相关文章推荐
- Mysql查询结果顺序按 in() 中ID 的顺序排列
- MySQL 存储过程传参数问题:实现 where id in(1,2,3,...)
- MySQL 存储过程传参数问题:实现 where id in(1,2,3,...)
- Mysql查询结果顺序按 in() 中ID 的顺序排列
- 由于外键的存在引发的一个mysql问题 Cannot change column 'id': used in a foreign key constraint
- 详解 Mysql查询结果顺序按 in() 中ID 的顺序排列
- MySQL 存储过程传参数问题:实现 where id in(1,2,3,...)
- mysql select *... where id in (select 字符串 from ... )查询结果问题?
- Mysql select in按原顺序排列问题
- Mysql 中 查询结果顺序按 IN 关键字中ID 的排列
- MySQL 存储过程传参数问题:实现 where id in(1,2,3,...)
- mysqli使用localhost问题 Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /mnt/www/
- 项目检出JRE问题(Unbound classpath container: 'JRE System Library [JavaSE-1.7]' in project 'idweb')
- mysql查询ID排序问题,
- Mysql获取id最大值、表的记录总数等相关问题的方法汇总
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- mysqli使用localhost问题 Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /mnt/www/
- mysql中or和in的效率问题
- [MySQL 工具] pt-query-digest使用出错问题分析Can't locate Time/HiRes.pm in @INC
- mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录