7

I want to search exact word from string like

id  Description
1   This is nice pen looking good
2   This is nice pendrive looking good

Search String : pen

My Current query

SELECT * FROM `table` WHERE Description like '%pen%';

Above Query return both record but I want Only first record. Because pen word exact match with my search string.

Expected Output

1   This is nice pen looking good

Demo

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122

4 Answers4

13

Try using regular expressions:

SELECT 
    *
FROM
    `table`
WHERE
    Description regexp '(^|[[:space:]])pen([[:space:]]|$)';

Demo

Or using word boundaries:

SELECT 
    *
FROM
    `table`
WHERE
    Description regexp '[[:<:]]pen[[:>:]]';
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
5

You can use REGEXP and the [[:<:]] and [[:>:]] word boundary markers:

SELECT
    *
FROM
    `table`
WHERE
    Description REGEXP '[[:<:]]pen[[:>:]]';

SQL Fiddle Demo

Faisal
  • 4,591
  • 3
  • 40
  • 49
1

you can use fulltext match like as follow

$query="SELECT * FROM table_name WHERE MATCH (column_name) AGAINST 
  ('search_word' IN NATURAL LANGUAGE MODE)" ;

you can select boolean mode also,it return exact word in search result

Ram
  • 115
  • 8
1

Try this to get exact word results

SELECT * FROM `table` WHERE Description ='pen';
  • 2
    That is full text, indeed, but if you look closer at the question you see that the question is really about finding a word in the description. – Robert Aug 24 '19 at 03:02