4

I am having problem in using mysql like keyword in certain condition. My requirement goes like this. First, when i search for 'ABC', the result should find ABC and ABCdef but not xyzABCdef or xyzABC. It seems simple at first to use ABC%. but in condition when i search for 'heart%', it doesnot return row that have 'liver heart' because it returns only row that have heart at the beginning of the string.Then i tried using % heart%.This returned row having 'liver heart' but not those rows that have heart at the beginning of string. I am kind of stuck here..so guys help me.

EDIT With your help guys i came with the following solution but still its giving me some problem.

    SELECT q.question_id, q.question, q.date,p.fname,p.lname,p.phys_id,
p.pic_path  
            FROM questions q JOIN physiciansprofile p ON p.phys_id=q.phys_id   
         WHERE  q.question  LIKE 'heart%' OR question LIKE '% heart%' 
            AND q.question LIKE 'liver%' OR q.question LIKE '% liver%' 
    ORDER BY q.date DESC LIMIT 0,10;

But this query return heart failure and symptoms liver as well.Is there any solution for this. I need to get result containing both heart and liver and also must satisfy condition as stated before. Is there any to solve this

1
  • can you post your whole code? Commented Jun 21, 2013 at 6:27

3 Answers 3

6

'% heart%' doesn't work because you are asking for anything, plus space, plus heart, plus anything.

Try something like:

like 'heart%' OR like '% heart%'
Sign up to request clarification or add additional context in comments.

Comments

3

I just saw this old unsolved request. As someone may have the same question, I'll post the answer in spite of the request's age.

WHERE q.question LIKE 'heart%' OR question LIKE '% heart%' 
AND q.question LIKE 'liver%' OR q.question LIKE '% liver%' 

translates to

WHERE q.question LIKE 'heart%' 
OR (question LIKE '% heart%' AND q.question LIKE 'liver%')
OR q.question LIKE '% liver%' 

because AND has precedence over OR.

So one must use parentheses here. This is often the case when mixing AND and OR and it's good habit to always do so:

WHERE (q.question LIKE 'heart%' OR question LIKE '% heart%')
AND (q.question LIKE 'liver%' OR q.question LIKE '% liver%')

Comments

1

what about

WHERE foo LIKE '% heart%' OR foo LIKE 'heart%'

7 Comments

using %heart% would match theart as well.So that cant be used
pardon sir? what exactly do you mean?
i mean %heart% would match xyzheart as well. I mean searching should be reasonable.If user enter hea than result should include heart but not theatre and so on.So using %s% is not correct in my case.
so your desired output is liver heart heart and heart failure?
i need result containing both heart and liver not only heart or liver.It must be and
|

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.