tirso
tirso

Reputation: 253

mysql select in multiple fields

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.

Upvotes: 0

Views: 841

Answers (3)

Max Shawabkeh
Max Shawabkeh

Reputation: 38683

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 <> '';

Upvotes: 1

Bjoern
Bjoern

Reputation: 16314

This shouts for a fulltext search.

Check out the mysql entries about that!

Upvotes: 0

H&#229;vard S
H&#229;vard S

Reputation: 23886

Just OR them together like so:

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

Upvotes: 0

Related Questions