
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL Query to Search Exact Word from String
To search exact word from string, use the below syntax −
select *from yourTableName where yourColumnName regexp '(^|[[:space:]])yourWord([[:space:]]|$)';
Let us first create a table −
mysql> create table DemoTable ( Title text ); Query OK, 0 rows affected (0.23 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('This is the Introduction to Java'); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable values('This is the Introduction to MongoDB'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('This is the Introduction to MySQL'); Query OK, 1 row affected (0.06 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------------------------------+ | Title | +-------------------------------------+ | This is the Introduction to Java | | This is the Introduction to MongoDB | | This is the Introduction to MySQL | +-------------------------------------+ 3 rows in set (0.00 sec)
Following is the query to search exact word from string.
mysql> select *from DemoTable where Title regexp '(^|[[:space:]])MySQL([[:space:]]|$)';
This will produce the following output −
+-----------------------------------+ | Title | +-----------------------------------+ | This is the Introduction to MySQL | +-----------------------------------+ 1 row in set (0.13 sec)
Advertisements