搜索超过 1 列 [英] Searching more than 1 column
问题描述
我为我的数据库设置了一个搜索功能来查找兄弟姐妹的名字.我遇到了一个问题,即兄弟姐妹列在不止一列中.我想知道您是否可以使用 WHERE 和 OR 来查看数据库中的 3 列或更多列.
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.
这就是我现在拥有的
SELECT * FROM movie WHERE $dropdown_1 LIKE '%$search%'"
dropdown_1
可以是几种不同的东西,包括兄弟姐妹,但我希望在 dropdown_1
中选择兄弟姐妹时,它会查看 siblings_1
,siblings_2
、siblings_3
和 siblings_4
以查看名称是否匹配.
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.
推荐答案
可能是时候考虑 规范化 您的数据库.
It's probably time to consider Normalizing your database.
要直接回答您的问题,您可以使用 OR
进行多个匹配
To directly answer your question, you can use OR
for multiple matches
`siblings_1` LIKE '%search%' OR `siblings_2` LIKE '%search%' # etc...
或者,正如 OMGPonnies 在下面的评论中指出的那样,使用 UNION
会更快:
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 :).
这篇关于搜索超过 1 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!