
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL REGEXP to fetch string + number records beginning with specific numbers?
For this, use REGEXP and fetch records beginning with specific numbers. Following is the syntax:
Select yourColumnName1,yourColumnName2 from yourTableName where yourColumnName2 REGEXP '^yourStringValue[yourNumericValue]';
Let us create a table −
mysql> create table demo45 -> ( −> id int not null auto_increment primary key, −> value varchar(50) −> ); Query OK, 0 rows affected (1.50 sec)
Insert some records into the table with the help of insert command. We are inserting records mixed with strings and numbers i.e. “John500, “John6500”, etc −
mysql> insert into demo45(value) values('John500'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo45(value) values('John1500'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo45(value) values('John5500'); Query OK, 1 row affected (0.42 sec) mysql> insert into demo45(value) values('John6500'); Query OK, 1 row affected (0.10 sec) mysql> insert into demo45(value) values('John8600'); Query OK, 1 row affected (0.19 sec)
Display records from the table using select statement −
mysql> select *from demo45;
This will produce the following output −
+----+----------+ | id | value | +----+----------+ | 1 | John500 | | 2 | John1500 | | 3 | John5500 | | 4 | John6500 | | 5 | John8600 | +----+----------+ 5 rows in set (0.00 sec)
Following is the query to fetch records with specific numbers i.e. 5 and 6 here −
mysql> select id,value −> from demo45 −> where value REGEXP '^John[56]';
This will produce the following output −
+----+----------+ | id | value | +----+----------+ | 1 | John500 | | 3 | John5500 | | 4 | John6500 | +----+----------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL RegExp to fetch records with only a specific number of words
- 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
- How to select records beginning with certain numbers in MySQL?
- MySQL query to select records beginning from a specific id
- MySQL LIKE command doesn't work with strings containing dots to display records beginning with a specific number
- Fetch ordered records after a specific limit in MySQL
- Fetch records containing a specific character twice in MySQL
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- MySQL to fetch records based on a specific month and year?
- How to use MySQL SELECT LEFT to fetch the records containing string with slash
- How to search specific word in MySQL with RegExp?
- UNIX_TIMESTAMP with date in MySQL query to fetch records after a specific date in different format?
- PHP fetch a specific value that begins with a given number from a string with letters and numbers?
- Order VARCHAR records with string and numbers in MySQL

Advertisements