5

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.
12
  • 1
    Are you sure you didn't use double quotes by accident?
    – John Gibb
    Jun 28, 2011 at 5:50
  • @John Gibb: Yeah, i am very sure. i used only single quotes
    – Romi
    Jun 28, 2011 at 5:52
  • If you want to use LIKE, just use it without the wildcard entries, select description from tproduct where description like 'diamond'
    – Balanivash
    Jun 28, 2011 at 5:53
  • @Balanivash: i tried it too , but is not working :(
    – Romi
    Jun 28, 2011 at 5:54
  • 2
    @Romi: I'm sure the query above is different than the one that you might be running. There is nothing wrong with the above query. But as your error message says, it is reading "diamond" as a column which can be if you are using back-ticks (`) instead of single-quotes (') around "diamond"
    – Abhay
    Jun 28, 2011 at 5:59

5 Answers 5

9

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

3

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

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

See REGEXP.

0
select description from tproduct where description like 'diamond'
1
  • @Romi,Can you give me the table structure please
    – gmhk
    Jun 28, 2011 at 6:29
0

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

2
  • Why does appending COLLATE SQL_Latin1_General_CP1_CS_AS help? Isn't it only the sort order? Feb 6, 2018 at 19:00
  • More generally, can you explain what it does and how it works (by editing your answer, not answering in comments)? Feb 6, 2018 at 19:01
-2
select description from tproduct where description = 'diamond'

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.