
- 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 return multiple row records with AND & OR operator
Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(40), StudentMathMarks int, StudentMySQLMarks int, status ENUM('ACTIVE','INACTIVE') ); Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Chris',45,67,'active'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Bob',89,78,'inactive'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('David',56,68,'active'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Robert',68,75,'active'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+------------------+-------------------+----------+ | StudentId | StudentName | StudentMathMarks | StudentMySQLMarks | status | +-----------+-------------+------------------+-------------------+----------+ | 1 | Chris | 45 | 67 | ACTIVE | | 2 | Bob | 89 | 78 | INACTIVE | | 3 | David | 56 | 68 | ACTIVE | | 4 | Robert | 68 | 75 | ACTIVE | +-----------+-------------+------------------+-------------------+----------+ 4 rows in set (0.00 sec)
Following is the query to return multiple row records with AND & OR operator −
mysql> select *from DemoTable where status='active' and (StudentMathMarks=68 or StudentMySQLMarks=67);
This will produce the following output −
+-----------+-------------+------------------+-------------------+--------+ | StudentId | StudentName | StudentMathMarks | StudentMySQLMarks | status | +-----------+-------------+------------------+-------------------+--------+ | 1 | Chris | 45 | 67 | ACTIVE | | 4 | Robert | 68 | 75 | ACTIVE | +-----------+-------------+------------------+-------------------+--------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL SELECT query to return records with specific month and year
- MySQL query to insert multiple records quickly
- MySQL query to return 5 random records from last 20 records?
- MySQL query for text search with LIKE and OR to fetch records
- MySQL query to find same value on multiple fields with LIKE operator?
- MySQL query to return all records with a datetime older than 1 week
- Delete selective multiple records using MySQL DELETE query
- MySQL update multiple records in a single query?
- MySQL query to display records from a table filtered using LIKE with multiple words?
- MySQL query to return all items in a single row
- MySQL query to insert row with date?
- Priority of AND and OR operator in MySQL select query?
- Fetch a specific record using a single MySQL query with AND & OR operator
- MySQL query to subtract date records with week day and display the weekday with records
- MySQL TINYINT type to return 1 or IS NULL records

Advertisements