Reputation: 1
I have a varchar field in mysql table and I want to search string in php which is alphanumeric right now it shows number only results.
For example if I search for 111 then it should show results matching 111( 111A, 111B, 111C). If I search for 111A then it should show results of 111A. How to achieve this.
Upvotes: 0
Views: 186
Reputation: 46900
Seems like your case is to the contrary of what LIKE does. It appears like you already use LIKE, and should not be using it for any exact match. Query may look like
SELECT myField from MyTable where myValue = '111'
SELECT myField from MyTable where myValue = '111A'
To achieve the opposite
You can use LIKE
SELECT myField from MyTable where myValue LIKE '111%'
This is for strings like 111Anything
SELECT myField from MyTable where myValue LIKE '%111%'
This is for strings like Anything111Anything
SELECT myField from MyTable where myValue LIKE '%111'
This is for strings like Anything111
Upvotes: 2
Reputation: 1689
Instead of LIKE
you can use REGEXP
For Start with 111
SELECT myField from MyTable where myValue REGEXP `^111`;
For End with 111
SELECT myField from MyTable where myValue REGEXP `111$`;
For Start with 111 and End with 111
SELECT myField from MyTable where myValue REGEXP `^111$`;
Upvotes: 0
Reputation: 633
Search patterns in php: from http://www.php.net/manual/en/function.preg-grep.php
<?php
$subject = array("111A", "111B", "222");
$pattern = "/^111/";
$matches = preg_grep($pattern, $subject);
print_r($matches);
?>
Upvotes: 1