读书人

如何从MYSQL的字符串字段中匹配一个字

发布时间: 2012-09-08 10:48:07 作者: rapoo

怎么从MYSQL的字符串字段中匹配一个字符???
表C_file,其中有个字段是spile,他存的是字符形式,例如:1,2,10,11

SQL code
 C_fileID     spile1      2,10,112      2,3,20,223      1,6,84      5,6,1,9

SQL code
 SQL:   select * from C_file where spile LIKE '%1%'
如果这样查询的话,会查询出ID为1、3、4,但正确的应该是3、4

那么这个SQL语句应该怎么写才正确的查询出1

[解决办法]
SQL code
select *from C_filewhere find_in_set(1,spile)
[解决办法]
SQL code
mysql> select * from test;+----+-----------+| id | name      |+----+-----------+|  1 | a,1,2     ||  2 | 2,3,20,30 ||  3 | 1,6,8     ||  4 | 5,6,1,9   ||  5 | e         |+----+-----------+5 rows in set (0.00 sec)mysql> select *from test where instr(concat(',',name,','),',1,')<>0;+----+---------+| id | name    |+----+---------+|  1 | a,1,2   ||  3 | 1,6,8   ||  4 | 5,6,1,9 |+----+---------+3 rows in set (0.00 sec)mysql> 

读书人网 >Mysql

热点推荐