“其中”语句:匹配单个字(不是子字符串) [英] "Where" statement: match a single word (not substring)
问题描述
我使用MySQL。
我在数据库中有一个汽车表,并且有一个名称
列
I have a car table in my database, and there is a name
column in that table.
假设表格的 name
列包含值:
+----------+
| name |
+----------+
| AAA BB |
----------
| CC D BB |
----------
| OO kk BB |
----------
| PP B CC |
----------
我要搜索表其中 name
列值包含字 BB (不是子字符串),什么是SQL命令来实现?
I would like to search the table where name
column value contains word "BB" (not substring), What is the SQL command to achieve this ?
我知道 LIKE ,但它用于匹配所包含的子字符串,而不是字词匹配。
I know LIKE , but it is used to match a contained substring, not for a word match.
PS
我的表包含大量数据。因此,我可能需要一种比使用LIKE更有效的方法
name
随机字符串。
请不要让我使用 IN(...),因为该列中的值是不可预测的。 p>
Please do not ask me to use IN (...) , because the values in that column is unpredictable.
推荐答案
尝试此WHERE子句:
Try this WHERE clause:
WHERE name LIKE '% BB %'
OR name LIKE 'BB %'
OR name LIKE '% BB'
OR name = 'BB'
请注意,如果表很大,这将不会很好。您还可以考虑全文搜索如果你需要更好的性能。
Note that this will not perform well if your table is large. You may also want to consider a full-text search if you need better performance.
这篇关于“其中”语句:匹配单个字(不是子字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!