Jump to content

Reverse matching


xenophobia

Recommended Posts

I have a mysql table with 2 fields:

id  |  prefix

The value will be something like:

1    |  T20

2    |  T22

3    |  T30

 

So when user key-in a value in the frontend: T20535664. I want it able to return me "1", since it is matching the first row of prefix field. How do I do that? I do not wish to use php's regex to retrieve all the rows and compare it one by one. Is SQL able to do that with the wildcard '%' ??

Link to comment
Share on other sites

Ermm sorry if my question is confusing. let me give you example:

 

$user_input = "T20535664";

// Something similiar
$sql = "SELECT id FROM table WHERE prefix LIKE '$user_input%'";

 

of course the sql above will never return '1' since it is not matching any of the prefix.

 

In regex:

("T20535664").match(/^prefix/);

 

This is what I want. But I have to retrieve all the prefix row by row before i can perform the searching.

 

Got what I mean?

Link to comment
Share on other sites

Ermmm first three digits should be enough to determine the ID.... But Im just afraid that going to increase...

 

So meaning in SQL we can achieve this?

 

Sorry, is "can't".

 

If the prefix is always three characters long, then just truncate the user input to three characters.

 

Nvm, is okay, I just wanna know whether there is a way in SQL. Seems like it doesn't.

 

Thanks you all.

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.