Jump to content

Search for string in column in mysql table separated by a character


Recommended Posts

I have a kind of search and react on my site. The user can search for a number in a column as below

$search = mysqli_real_escape_string($link, $_POST["query"]);

$query = "SELECT * FROM $table[$i] WHERE oemnr LIKE '".$search."%' ORDER BY model";

 

The code works fine but I want to expand the function to get match for other numbers also. The oemnr column today can be 12345 which shows up if the user type 12.... 

I want to expand the oemnr column as

12345/03124/34713, numbers separated with / or another character.

If the user type 03... or 34... I want to find and show 12345 because it is on the same row and corresponds to the numbers on the same row. Is it possible to do in mysql? In php that kind of operation can be done with the explode function but I don't know how to use with mysql.

Edited by jigga
Link to post
Share on other sites
1 hour ago, jigga said:

I want to expand the oemnr column as

12345/03124/34713, numbers separated with / or another character.

Don't.

Put your oemnr in a separate table, one per row with the id of the parent record as a foreign key.

In other words, normalize your data.

+------------------+                    
| main_table       |                    
+------------------+               +------------------+     
| id     (PK)      |-------+       | oem_number       |     
| etc...           |       |       +------------------+     
+------------------+       |       |  id      (PK)    |
                           +------<|  table_id  (FK)  |
                                   |  oemnr           |
                                   +------------------+

 

Link to post
Share on other sites

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.