Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query for exact match of a string in SQL

Tags:

sql

mysql

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 card query. For example, I got ringdiamond, diamondmaster, etc.

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'

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.
like image 574
Romi Avatar asked Jun 28 '11 05:06

Romi


People also ask

What is exact string match?

The Exact String Match comparison is a simple comparison that determines whether or not two String/String Array values match. Use the Exact String Match comparison to find exact matches for 2 values for a String identifier.

How do I find similar text in SQL?

Using the Levenshtein distance method This method can be used among others (Soundex, LIKE statement, Regexp) to perform string similarity or string matching in order to identify two elements (text, strings, inputs) that are similar but not identical.

Are used in SQL to match a string pattern?

SQL pattern matching enables you to use _ to match any single character and % to match an arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case-insensitive by default. Some examples are shown here. Do not use = or <> when you use SQL patterns.

Is there a match function in SQL?

Specifies a search condition for a graph. MATCH can be used only with graph node and edge tables, in the SELECT statement as part of WHERE clause.


2 Answers

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

like image 177
ratsbane Avatar answered Sep 20 '22 15:09

ratsbane


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

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

See REGEXP.

like image 35
Bill Karwin Avatar answered Sep 19 '22 15:09

Bill Karwin