0

In my search I used autosuggest. The problem now is I have to search the value in multiple fields such as firstname,middlename,lastname,caption etc. How to identify that the match string will belong on specific field name.

let say i have table

 firstname     middlename     lastname      caption
 james         kelly          tow           handsome
 timy          john           fung          fearless
 hanes         bing           ken           great

in my input field once I typed "j" I should select and ouput

james
john
great

Currently I just output the firstname so instead of the above result I came out like below which is not good.

james
timy
hanes

It is possible? Any help would greatly appreciated.

3 Answers 3

1

You could do something like:

SELECT IF(first LIKE 'j%',
          first,
          IF(middle LIKE 'j%',
             middle,
             IF(last LIKE 'j%',
                last,
                ''))) AS name
FROM mytable
HAVING name <> '';
Sign up to request clarification or add additional context in comments.

1 Comment

hi max, your solution is so nice and works for me. I tried before the fulltext search and using OR but the problem is I don't know in which fieldname belong. Thanks
0

Just OR them together like so:

SELECT * FROM yourTable WHERE firstname LIKE '%j%' OR lastname LIKE '%j%' -- ...

Comments

-1

This shouts for a fulltext search.

Check out the mysql entries about that!

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.