怎么从MYSQL的字符串字段中匹配一个字符???
表C_file,其中有个字段是spile,他存的是字符形式,例如:1,2,10,11
SQL code
SQL code
那么这个SQL语句应该怎么写才正确的查询出1
SQL code
C_file ID spile 1 2,10,11 2 2,3,20,22 3 1,6,8 4 5,6,1,9
SQL code
SQL: select * from C_file where spile LIKE '%1%'如果这样查询的话,会查询出ID为1、3、4,但正确的应该是3、4
那么这个SQL语句应该怎么写才正确的查询出1
作者: copier 发布时间: 2011-05-13
SQL code
select * from C_file where find_in_set(1,spile)
作者: ACMAIN_CHM 发布时间: 2011-05-13
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>
作者: lxq19851204 发布时间: 2011-05-13
太感谢了,good,解决了
作者: copier 发布时间: 2011-05-13
两种办法都可以,1楼的最方便
作者: copier 发布时间: 2011-05-13