mysql中替代charindex的函数substring_index、find_in_set
2012-10-26 19:11
591 查看
表结构如下:
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
+----+-------+
执行以下SQL:
mysql> select * from test where id in(3,1,5);
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 3 | test3 |
| 5 | test5 |
+----+-------+
3 rows in set (0.00 sec)
这个select在mysql中得结果会自动按照id升序排列,
但是我想执行"select * from test where id in(3,1,5);"的结果按照in中得条件排序,即:3,1,5,
想得到的结果如下:
id name
3 test3
1 test1
5 test5
请问在这样的SQL在Mysql中怎么写?
网上查到sqlserver中可以用order by charindex解决,但是没看到Mysql怎么解决??请高手帮忙,谢
谢!
select * from a order by substring_index('3,1,2',id,1);
试下这个good,ls正解。
order by find_in_set(id,'3,1,5')
谢谢,经测试order by substring_index和order by find_in_set都可以
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
+----+-------+
执行以下SQL:
mysql> select * from test where id in(3,1,5);
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 3 | test3 |
| 5 | test5 |
+----+-------+
3 rows in set (0.00 sec)
这个select在mysql中得结果会自动按照id升序排列,
但是我想执行"select * from test where id in(3,1,5);"的结果按照in中得条件排序,即:3,1,5,
想得到的结果如下:
id name
3 test3
1 test1
5 test5
请问在这样的SQL在Mysql中怎么写?
网上查到sqlserver中可以用order by charindex解决,但是没看到Mysql怎么解决??请高手帮忙,谢
谢!
select * from a order by substring_index('3,1,2',id,1);
试下这个good,ls正解。
order by find_in_set(id,'3,1,5')
谢谢,经测试order by substring_index和order by find_in_set都可以
相关文章推荐
- mysql中对order by的函数substring_index() , find_in_set()使用
- mysql 函数 find in set()
- mysql中find_in_set()函数的使用详解
- MySQL的FIND_IN_SET()函数
- MySQL 的 find_in_set 函数使用方法
- mysql中find_in_set()函数的使用
- mysql中find_in_set()函数的使用
- mysql中find_in_set()函数的使用
- MySql的FIND_IN_SET()查询函数的使用
- mysql通过find_in_set()函数实现where in()顺序排序
- mysql 函数FIND_IN_SET、group_concat 、concat、cast的高级应用
- mysql 多值检索 find_in_set()函数
- mysql中find_in_set()函数的使用
- kphp企业站快速开发之关联字段查询方法:mysql的find_in_set()函数
- mysql中find_in_set()函数的使用
- mysql 的 find_in_set 函数使用方法
- mysql 实现按 where in () 中的顺序排序,用find_in_set() 函数
- MySQL中的find_in_set(A, B)函数,判断字符串A是否存在B中
- oracle:与mysql相似得find_set_in函数用法
- mysql中find_in_set()函数的使用及in()用法详解