MySQL 在 SQL 中查询字符串的精确匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6502134/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:20:44  来源:igfitidea点击:

Query for exact match of a string in SQL

mysqlsql

提问by Romi

I want to query for the exact match of a string from a database table.

我想从数据库表中查询字符串的精确匹配。

When I write the query as

当我将查询写为

select description from tproduct where description like '%diamond%'

it runs and gives the results as a wild cardquery. For example, I got ringdiamond, diamondmaster, etc.

它运行并以wild card查询的形式给出结果。例如,我得到了ringdiamond、diamondmaster等。

But I want to get only "diamond".

但我只想得到“钻石”。

For this I did as:

为此,我是这样做的:

select description from tproduct where description = 'diamond'

But it is giving an error:

但它给出了一个错误:

Error: Unknown column 'diamond' in 'where clause'

错误:“where 子句”中的未知列“diamond”

The description column contains:

描述栏包含:

This bracelet is composed of round diamond surrounded by milgrain detailing. Its secure setting prevents it from twisting and ensures it will sit impeccably on her wrist.


This stylish design is the perfect accessory for the day or evening. Akoya cultured pearls are lined up by a string of bezel-set round diamond.

回答by ratsbane

If I understand the question correctly you want to match "diamond" when it's a distinct word and not part of another word like "diamondville." You could do something like SELECT * FROM tproduct WHERE description like '% diamond %' and this would match all of the records which have "diamond" surrounded by spaces.

如果我正确理解了这个问题,那么当“diamond”是一个不同的词而不是像“diamondville”这样的另一个词的一部分时,您想要匹配它。您可以执行类似 SELECT * FROM tproduct WHERE description like '% diamond %' 之类的操作,这将匹配所有“菱形”被空格包围的记录。

But that wouldn't work. That wouldn't find records where the description starts with "Diamond" or where there's a comma or period after "Diamond"

但这行不通。这不会找到描述以“Diamond”开头或“Diamond”后有逗号或句点的记录

You need to match on a regular expression. You can specify word boundaries with that:

您需要匹配正则表达式。您可以指定单词边界:

select * from t2 where description regexp '[[:<:]]diamond[[:>:]]';

See this page for more info on MySQL regular expressions: http://dev.mysql.com/doc/refman/5.1/en/regexp.html

有关 MySQL 正则表达式的更多信息,请参阅此页面:http: //dev.mysql.com/doc/refman/5.1/en/regexp.html

回答by Bill Karwin

You can use a regular expression with a special pattern for word boundaries.

您可以使用带有特殊模式的正则表达式作为单词边界。

select description from tproduct 
where description regexp '[[:<:]]diamond[[:>:]]'

See REGEXP.

REGEXP

回答by Srinu Babu

For SQL Server: Description
SELECT description FROM tproduct WHERE description ='diamond' COLLATE SQL_Latin1_General_CP1_CS_AS

对于 SQL Server: 描述
SELECT description FROM tproduct WHERE description ='diamond' COLLATE SQL_Latin1_General_CP1_CS_AS

回答by gmhk

select description from tproduct where description like 'diamond'

回答by Mudassir

select description from tproduct where description = 'diamond'