- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 RegExp to fetch records with only a specific number of words
For this, use Regular Expression in MySQL as in the below syntax −
select * from yourTableName where yourColumnName regexp '\land[\land ]+[ ]+[\land ]+$';
The above query will work when the two words are separated by a space. Let us first create a table −
mysql> create table DemoTable1412 -> ( -> Name varchar(40) -> ); Query OK, 0 rows affected (0.52 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1412 values('John Adam Carol'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1412 values('Mike Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1412 values('Chris James Robert'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1412;
This will produce the following output −
+--------------------+ | Name | +--------------------+ | John Adam Carol | | Mike Sam | | Chris James Robert | +--------------------+ 3 rows in set (0.00 sec)
Here is the query to check if a certain row has two words separated by space −
mysql> select * from DemoTable1412 where Name regexp '\land[\land ]+[ ]+[\land ]+$';
This will produce the following output −
+----------+ | Name | +----------+ | Mike Sam | +----------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL REGEXP to fetch string + number records beginning with specific numbers?
- Implement MySQL REGEXP to fetch records with . and numbers
- MySQL regexp to display only records with strings or strings mixed with numbers. Ignore only the number records
- Create a Stored Procedure with MySQL and set a limit to display only a specific number of records
- Fetch records containing a specific character twice in MySQL
- Fetch ordered records after a specific limit in MySQL
- MySQL to fetch records based on a specific month and year?
- How to escape parentheses in MySQL REGEXP clause and display only specific values with parentheses?
- UNIX_TIMESTAMP with date in MySQL query to fetch records after a specific date in different format?
- How to search specific word in MySQL with RegExp?
- MySQL query to fetch records where decimal is a whole number
- MySQL query to select all the records only from a specific column of a table with multiple columns
- How can fetch records from specific month and year in a MySQL table?
- MySQL query to fetch specific records matched from an array (comma separated values)
- MySQL db query to fetch records from comma separate values on the basis of a specific value

Advertisements