terry
terry

Reputation: 17

Searching more than 1 column

I have set up a a search function for my database to look for names of siblings. I have run into a problem in that the siblings are listed in more than one column. I was wondering if you can use the WHERE with OR to look into 3 or more columns in a data base.

This is what i have now

SELECT * FROM movie WHERE $dropdown_1 LIKE '%$search%'"

The dropdown_1 can be several different things including siblings but I would like when siblings is selected in dropdown_1 it will look at siblings_1, siblings_2, siblings_3 and siblings_4 to see if a name matches.

Upvotes: 1

Views: 94

Answers (2)

Andreas Wong
Andreas Wong

Reputation: 60594

It's probably time to consider Normalizing your database.

To directly answer your question, you can use OR for multiple matches

`siblings_1` LIKE '%search%' OR `siblings_2` LIKE '%search%' # etc...

Alternatively, as OMGPonnies pointed out in the comment below, using UNION is faster:

WHERE siblings_1 LIKE '%search%' UNION SELECT ... WHERE siblings_2 LIKE '%search%' # etc ...

But I insist, if you can still help it, to redesign your database :).

Upvotes: 3

Paul
Paul

Reputation: 1088

Putting column names in user-defined variables is a bad idea, as then they are able to search anywhere in your table.

What you probably need is more like

SELECT * FROM movie WHERE `dropdown_column` LIKE '%$search%'" OR `sibling_column` LIKE '%$search%'" etc..

Upvotes: 1

Related Questions