flower
flower

Reputation: 2242

What's wrong with my sql when I using FIND_IN_SET function?

In my table called testtb has a column that storing file path,Now I want to find the filed which start in C:\.I use the sql,but it always return 0.what's wrong?

--example 
select  FIND_IN_SET('C:\\', 'C:\\abc.png' )
-- my sql
 select  FIND_IN_SET('C:\\', filepath) from testtb;

Here is my table data:

id,    filepath,               srcfilepath
'1', 'C:\20160101\abc.jpg', 'C:\20160101\abc.jpg'
'2', 'D:\20160101\abc.jpg', 'D:\20160101\abc.jpg'
'3', 'E:\TP\20160101\abc.jpg', 'E:\TP\20160101\abc.jpg'

Upvotes: 1

Views: 282

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270623

You are just confused. find_in_set() refers to sets of values in MySQL. In MySQL, these are values separated by commas: '1,2,3' or 'abc,def,ghi'.

I think you are looking for the instr() or position() function:

select instr('C:\\abc.png', 'C:\\')

Upvotes: 2

Dylan Su
Dylan Su

Reputation: 6065

find_in_set is not proper here, and like will do what you expect.

mysql> select  'C:\\abc.png' LIKE 'C:\\\\%';
+------------------------------+
| 'C:\\abc.png' LIKE 'C:\\\\%' |
+------------------------------+
|                            1 |
+------------------------------+
1 row in set (0.00 sec)

Upvotes: 1

Related Questions