
- 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 Select when a grouped record has multiple matching strings?
You can use regular expression for this. Let us first create a table −
mysql> create table DemoTable ( ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ProductName varchar(20) ); Query OK, 0 rows affected (0.19 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ProductName) values('Product-1'); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(ProductName) values('Product2'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(ProductName) values('Product1'); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(ProductName) values('Product-3'); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(ProductName) values('Product3'); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(ProductName) values('Product-4'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(ProductName) values('Product4'); Query OK, 1 row affected (0.05 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+ | ProductId | ProductName | +-----------+-------------+ | 1 | Product-1 | | 2 | Product2 | | 3 | Product1 | | 4 | Product-3 | | 5 | Product3 | | 6 | Product-4 | | 7 | Product4 | +-----------+-------------+ 7 rows in set (0.00 sec)
Following is the query for a grouped record having multiple matching strings −
mysql> select *from DemoTable where ProductName regexp 'Product[1234].*';
This will produce the following output −
+-----------+-------------+ | ProductId | ProductName | +-----------+-------------+ | 2 | Product2 | | 3 | Product1 | | 5 | Product3 | | 7 | Product4 | +-----------+-------------+ 4 rows in set (0.00 sec)
- Related Articles
- Insert record using MySQL SELECT?
- Update record on a specific date matching the current date in MySQL
- MySQL Select Multiple VALUES?
- How to select a random record from a MySQL database?
- MySQL query to select a record with two exact values?
- Select last record and update it in MySQL?
- MySQL select * and find record with current date
- How to select record except the lower value record against a specific value in MySQL?
- How to select row when column must satisfy multiple value in MySQL?
- MySQL select query with multiple WHERE?
- How to get a specific column record from SELECT query in MySQL?
- Matching strings with a wildcard in C#
- MySQL Select Statement DISTINCT for Multiple Columns?
- MySQL query to select multiple rows effectively?
- How to execute multiple select queries in MySQL ?

Advertisements