MySQL “Where”语句:包含某个子串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8152548/
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
"Where" statement : contains a certain substring
提问by Mellon
I am using MySQL.
我正在使用 MySQL。
I have a cartable in my database, and there is a name
column in that table.
我的数据库中有一个car表,该表中有一个name
列。
Suppose the name
column of the table contain values:
假设name
表的列包含值:
+----------+
| name |
+----------+
| AAA BB |
----------
| CC D BB |
----------
| OO kk BB |
----------
| PP B CC |
----------
I would like to search the table where name
column value contains "BB" , What is the SQL command to achieve this ?
我想搜索name
列值包含“ BB”的表,实现此目的的 SQL 命令是什么?
I tried :
我试过 :
SELECT * FROM car WHERE name LIKE "BB";
But it does not work.
但它不起作用。
P.S.
聚苯乙烯
The values in name
column are random strings.
name
列中的值是随机字符串。
Please do not ask me to use IN (...), because the values in that column is unpredictable.
请不要让我使用IN (...),因为该列中的值是不可预测的。
---------Please close this question--------------
---------请关闭此问题--------------
Sorry, I think I asked the question wrongly. Actually I am asking for a word match not a substring.
抱歉,我想我问错了问题。实际上,我要求的是单词匹配而不是子字符串。
回答by Matteo Alessani
You need to include the %
operator to select records that contain "BB" in the name field, not only the exact value.
您需要包含%
运算符来选择名称字段中包含“BB”的记录,而不仅仅是精确值。
SELECT * FROM car WHERE name LIKE "%BB%";