Forum Moderators: coopster

Message Too Old, No Replies

Query to find "foo" within "(foo)"

SQL queries don't match words next to Parentheses

         

guarriman

5:31 pm on Sep 19, 2007 (gmt 0)

10+ Year Member



Hi.

I've got this table in mySQL:

item 1:
-- firstname: John (Johnie)
-- phone: 555-600-200

item 2:
-- firstname: Peter
-- phone: 555-300-400

I created this SQL query to find 'johnie':


SELECT friends.id FROM friends WHERE ((friends.firstname LIKE '% johnie %' OR friends.firstname LIKE 'johnie %' OR friends.firstname LIKE '% johnie' OR friends.firstname LIKE 'johnie' OR friends.phone LIKE '% johnie %' OR friends.phone LIKE 'johnie %' OR friends.phone LIKE '% johnie' OR friends.phone LIKE 'johnie')) ORDER BY friends.firstname LIMIT 0, 9999

But it doesn't match anything, because it's consdiers "(johnie)" as a single word. Is there any way to make mySQL consider "(johnie)" as "johnie".

I know I can create another condition within my query:
firstname LIKE '(johnie)' OR firstname LIKE '(johnie%' OR firstname LIKE '%johnie)'
but I also might consider other characters like ' " - *

Any suggestion?

d40sithui

6:20 pm on Sep 19, 2007 (gmt 0)

10+ Year Member



i duno if this is it, but it seems to me that you should NOT have a space in the "% johnie %" search. try without the spaces "%johnie%" and see how it works.

guarriman

7:27 am on Sep 20, 2007 (gmt 0)

10+ Year Member



Nope. If I add '%johnie%', it would match with 'johnies', 'aljohnie' or 'myjohnieos' and I don't want it. Blanks are mandatories.

Habtom

7:35 am on Sep 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the top of my mind, I think something like this might work:

'% [(]johnie[)] %'

Habtom

vincevincevince

8:09 am on Sep 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



WHERE friends.phone REGEXP '[[:<:]]johnie[[:>:]]';