
- 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 alternative records from a table
To find alternative records from a table, you need to use the OR condition as in the below syntax −
select *from yourTableName where yourColumnName=yourValue1 OR yourColumnName=yourValue2…...N;
Let us first create a table −
mysql> create table DemoTable772 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(100), Age int ); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable772(Name,Age) values('Chris',21); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable772(Name,Age) values('Robert',26); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable772(Name,Age) values('Mike',27); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable772(Name,Age) values('Sam',24); Query OK, 1 row affected (0.45 sec) mysql> insert into DemoTable772(Name,Age) values('Carol',28); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable772(Name,Age) values('David',25); Query OK, 1 row affected (0.93 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable772;
This will produce the following output -
+----+--------+------+ | Id | Name | Age | +----+--------+------+ | 1 | Chris | 21 | | 2 | Robert | 26 | | 3 | Mike | 27 | | 4 | Sam | 24 | | 5 | Carol | 28 | | 6 | David | 25 | +----+--------+------+ 6 rows in set (0.00 sec)
Here is the query for MySQL OR −
mysql> select *from DemoTable772 where Id=2 OR Id=4 OR Id=6;
This will produce the following output -
+----+--------+------+ | Id | Name | Age | +----+--------+------+ | 2 | Robert | 26 | | 4 | Sam | 24 | | 6 | David | 25 | +----+--------+------+ 3 rows in set (0.01 sec)
- Related Articles
- Delete records from a MySQL table with IN() in a single query
- MySQL query to fetch the latest date from a table with date records
- MySQL query to display records from a table filtered using LIKE with multiple words?
- MySQL query to copy records from one table to another with different columns
- A single MySQL query to insert records (not all) in the second table from the first table
- MySQL query to select records from a table on the basis of a particular month number?
- MySQL query to find a match and fetch records
- MySQL query to return 5 random records from last 20 records?
- MySQL query to select records beginning from a specific id
- MySQL query to fetch records from a range of months?
- Delete all records from a table in MySQL?
- Delete all the records from a MySQL table?
- MySQL query to select all the records only from a specific column of a table with multiple columns
- Create MySQL query to create a table from an existing table?
- MySQL query to display the count of distinct records from a column with duplicate records

Advertisements