MySQL MYSQL中带有多个字段和空格的LIKE通配符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7129355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LIKE wildcard with multiple fields and spaces in MYSQL
提问by penpen
I'm having some trouble searching for any similar match in two fields. For example I have a table with the values:
我在两个字段中搜索任何类似的匹配项时遇到了一些麻烦。例如,我有一个包含值的表:
CAR MAKE CAR MODEL
Ford Mustang (Shelby)
Toyota Corolla
Seat Leon
etc etc.
I want to be able to get the result "Ford, Mustang (Shelby)" by searching for any of the following combinations:
我希望能够通过搜索以下任何组合来获得结果“Ford, Mustang (Shelby)”:
- Ford
- Mustang
- Shelby
Ford Mustang
or any other combination.
- 福特
- 野马
- 谢尔比
福特野马
或任何其他组合。
Is this possible? I've had a good search but it's hard to find the search terms to describe what I mean.
这可能吗?我有一个很好的搜索,但很难找到描述我的意思的搜索词。
回答by mu is too short
Split your terms on whitespace and then, for each term, build a little bit of SQL like this:
在空格上拆分您的术语,然后为每个术语构建一些 SQL,如下所示:
car_make like '%x%' or car_model like '%x%'
Then join all of those with or
to get your WHERE clause. So for "Shelby Ford", you'd end up with SQL like this:
然后加入所有这些or
以获取您的 WHERE 子句。所以对于“Shelby Ford”,你最终会得到这样的 SQL:
select stuff
from cars
where car_make like '%Shelby%'
or car_model like '%Shelby%'
or car_make like '%Ford%'
or car_model like '%Ford%'
If you need anything more complicated then investigate MySQL's full-text searchcapabilities.
如果您需要更复杂的东西,请研究 MySQL 的全文搜索功能。
回答by Banjoe
Give this a try:
试试这个:
SELECT Car_Make, Car_Model, CONCAT_WS(' ', Car_Make, Car_Model) as Make_Model
FROM cars
WHERE CONCAT_WS(' ', Car_Make, Car_Model) LIKE '%Ford Mustang%'
Not sure of the exact syntax since I'm not at home but something similar should work.
不确定确切的语法,因为我不在家,但类似的东西应该可以工作。
See also: Using mysql concat() in WHERE clause?