mysql like syntax with backslash

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
thomas
Forum Newbie
Posts: 6
Joined: Wed May 21, 2008 3:18 pm

mysql like syntax with backslash

Post by thomas »

Hi,

I have a database table name "test" with one field name "fname" and in that table I have an entry "abc\123". I tried running the queries below and got no result.

select * from test where fname like '%abc\123%'
or
select * from test where fname like '%abc\\123%'

I'm not sure if the mysql like syntax have any problem with backslashes but is there a way to get around this?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: mysql like syntax with backslash

Post by mikosiko »

thomas wrote:Hi,

I have a database table name "test" with one field name "fname" and in that table I have an entry "abc\123". I tried running the queries below and got no result.

select * from test where fname like '%abc\123%'
or
select * from test where fname like '%abc\\123%'

I'm not sure if the mysql like syntax have any problem with backslashes but is there a way to get around this?
select * from test where fname like '%abc%'

or
select * from test where fname like '%abc%123%'
could do the job... but also can give you unwanted records... per example this records will be returned also:
fname="abcjsd jsd j jsd jhs"
faname="abc shdjhjashd 123 hsdhjashd"

and this
select * from test where fname like '%abc_123%'
should solve your pattern
Post Reply