Click to See Complete Forum and Search --> : Query in MySQL with two search terms


Thiele
05-21-2009, 04:58 AM
Hi there

Can anybody tell me how to make a query in MySQL using two search-terms?

What I want to do is:
I have a field in my table, containing keywords seperated by a comma.
In my php search field I want to be able to search by two or more criteria ex. CALLAWAY and FTIQ (from the same field in the table).
I can do a search for callaway and I can do a search for ftiq which both return a result, but I don't know how to combine the two in one search. What would be the correct syntax? is it 'and' or '%' or what the he.. is it.

Best regards
Anders Thiele

NogDog
05-21-2009, 09:18 AM
If you want to use a LIKE comparison, it could be something such as:

SELECT . . . WHERE some_column LIKE '%callaway%' OR some_column LIKE '%FTIQ%'

xvszero
05-21-2009, 02:24 PM
This is kind of why it is a bad idea to have a single field for keywords. A better idea is probably a separate table with an entry for each keyword (linking it by an ID or something.)

You know, instead of...

MAIN TABLE
ID KEYWORDS
1 keyword1, keyword2
2 keyword17
3 keyword423, keyword424

You have...

MAIN TABLE
ID
1
2
3

KEYWORD TABLE
ID KEYWORD
1 keyword1
1 keyword2
2 keyword17
3 keyword423
3 keyword424

Etc. Then searching on keywords is a lot easier.