
- 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 query to find a match and fetch records
To find a match from records, use MySQL IN(). Let us first create a table −
mysql> create table DemoTable ( Id int, FirstName varchar(20), Gender ENUM('Male','Female') ); Query OK, 0 rows affected (1.73 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(1,'Chris','Male'); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable values(10,'Emma','Female'); Query OK, 1 row affected (1.88 sec) mysql> insert into DemoTable values(9,'Emma','Male'); Query OK, 1 row affected (0.70 sec) mysql> insert into DemoTable values(11,'Isabella','Female'); Query OK, 1 row affected (0.46 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+-----------+--------+ | Id | FirstName | Gender | +------+-----------+--------+ | 1 | Chris | Male | | 10 | Emma | Female | | 9 | Emma | Male | | 11 | Isabella | Female | +------+-----------+--------+ 4 rows in set (0.00 sec)
Following is the query to find match and fetch records with Id 1 and 11 −
mysql> select *from DemoTable where Id IN(1,11);
This will produce the following output −
+------+-----------+--------+ | Id | FirstName | Gender | +------+-----------+--------+ | 1 | Chris | Male | | 11 | Isabella | Female | +------+-----------+--------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to fetch records from a range of months?
- MySQL query to fetch records before currentdate + 2 weeks?
- MySQL query to fetch records where decimal is a whole number
- MySQL query for text search with LIKE and OR to fetch records
- MySQL query to fetch records wherein timestamp is before 15+ days?
- MySQL query to fetch the latest date from a table with date records
- MySQL query to fetch specific records matched from an array (comma separated values)
- MySQL query to find alternative records from a table
- Implement MySQL REGEXP to fetch records with . and numbers
- MySQL to fetch records based on a specific month and year?
- MongoDB query to fetch date records (ISODate format) in a range
- UNIX_TIMESTAMP with date in MySQL query to fetch records after a specific date in different format?
- How to order records and fetch some using MySQL LIMIT?
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- MySQL query to fetch records with arrangement in the form of numbers and letter like 99S, 50K, etc.?

Advertisements