4

I am trying to select a record using the LIKE function. However, it does not show results if there are characters in between. I want it to select data that contains those strings. For example:

 $value = "Mark Anthony";
 $qry ="SELECT * FROM students WHERE name LIKE '%$value%'";

returns these results:

 John Mark Anthony
 Mark Anthony Philipps

but I also want to have results like these

 Mark James Anthony
 Mark Gabriel Anthony Fernandez
 Marko Julian Anthonyo

Any ideas? UPDATE: 'Mark' must be before 'Anthony'

5 Answers 5

6

I think Full TEXT works

But Full-text searches are supported for MyISAM tables only

SELECT * FROM students 
  WHERE MATCH (name)
   AGAINST ('Mark Anthony' IN BOOLEAN MODE)

UPDATE:- As per question update OP need to search Marko as well, then you can get like this:-

SELECT * FROM students 
WHERE
(
    name LIKE '%Mark%'
    OR name LIKE '%Anthony%'
)
Sign up to request clarification or add additional context in comments.

4 Comments

It's okay, I am using PHPmyadmin anyway. Will try this out.
Yes try it, I think in your case FULL TEXT is best
Updated my question. Will results like these appear? Marko Julian Anthonyo
Hi thanks for the answer :) Although I have already chosen an answer. Will upvote instead. Thanks!
2

You could split the value string into two parts. Like so:

WHERE name LIKE '%Mark%' AND name LIKE '%Anthony%'

3 Comments

Hi there, I haven't updated the question but Mark has to be before Anthony. With your suggestion I think it will include results with Anthony before Mark. :)
Yeah thats a possibility with my query. '%Mark%Anthony%' should do the trick tho.. @Elmer
Updated my question. Will results like these appear? Marko Julian Anthonyo
1

last try:

$value = "Mark Anthony";
$value = str_replace(" ","%",$value);
$qry ="SELECT * FROM students WHERE name LIKE '%$value%'";

not tested!

4 Comments

Updated my question. Do you think it will also include results like this? Marko Julian Anthonyo?
Just tested it. No results were found. xD
Just a blank page. Not even errors. But your new answer works great! Why didn't I think of str_replace xD Thanks man!
nice to hear. as you see my first answer was a bit overloaded too ;-)
1

Try this solution -

SELECT * FROM table WHERE column REGEXP '(Mark).+(Anthony)';

2 Comments

Haven't tried this yet but was wondering if there was a solution that wouldn't be fixed to Mark Anthony only :) Like if I just want to look for 'Mark'.
Why haven't you tried this? Regex function sometimes can help.
0

The proplem is, you say "bring all results which contains Mark Anthony" so it does. You should set it like

$value = "Mark%Anthony";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.