Query for exact match of a string in SQL

前端 未结 5 2029
谎友^
谎友^ 2021-01-06 00:47

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          


        
相关标签:
5条回答
  • 2021-01-06 00:59

    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.

    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"

    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

    0 讨论(0)
  • 2021-01-06 01:07
    select description from tproduct where description like 'diamond'
    
    0 讨论(0)
  • 2021-01-06 01:22

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

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

    See REGEXP.

    0 讨论(0)
  • 2021-01-06 01:23

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

    0 讨论(0)
  • 2021-01-06 01:24
    select description from tproduct where description = 'diamond'
    
    0 讨论(0)
提交回复
热议问题