Jump to content

[SOLVED] Pattern Matching Query


GeneralBill

Recommended Posts

I would appreciate it if someone could help me come up with a query.  Let's pretend that I have a schools table and that I want to fetch an id given a name.  However, I don't have the exact name.  Instead, I have a name that has all the non alphanumeric / whitespace characters stripped from it.

 

SCHOOLS TABLE

id, name

1, Plantation High-Schools

2, Cat & Dog Schools

3, Culinary / Cooking Schools

 

I have this information available:

$name = plantation high schools

$name = cat dog schools

$name = culinary cooking schools

 

Let's say that I wanted to get the id for $name.  What would the query look like?  I assume it might look something like this:

 

SELECT id FROM schools WHERE name RLIKE '';

Link to comment
Share on other sites

$query = "SELECT `id` FROM `schools` WHERE `name` LIKE '$name'";

 

The above query unfortunately is not what I'm looking for.

 

$name = "cat dog schools";

$query = "SELECT `id` FROM `schools` WHERE `name` LIKE '$name'";

 

That would not return any results.  I want it to return id = 2.

 

I probably need to use some kind of REGEXP or RLIKE pattern.  Anyone else out there have any ideas?

Link to comment
Share on other sites

I figured it out.  I just needed to format this:

 

$name = "cat dog schools";

 

To look like this:

 

$name = "^cat[^[:alnum:]]+dog[^[:alnum:]]+schools$"

 

Then, this will work:

 

select id from schools where name rlike '$name';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.