

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query with OR & AND conditions
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20), -> Age int -> ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name,Age) values('John',21); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name,Age) values(Null,20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name,Age) values('David',23); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name,Age) values('Carol',null); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | John | 21 | | 2 | NULL | 20 | | 3 | David | 23 | | 4 | Carol | NULL | +----+-------+------+ 4 rows in set (0.00 sec)
Following is the query with OR & AND conditions −
mysql> select *from DemoTable -> where Name='David' OR (Name is null AND Age=20);
Output
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 2 | NULL | 20 | | 3 | David | 23 | +----+-------+------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL query to return multiple row records with AND & OR operator
- How to order and select query with conditions in MySQL?
- MongoDB Query to combine AND & OR?
- Fetch a specific record using a single MySQL query with AND & OR operator
- Can we use WHERE, AND & OR in a single MySQL query?
- MongoDB query to get documents with multiple conditions set in $or?
- MySQL query to display records on the basis of conditions IS NULL OR !=1;?
- Set conditions for columns with values 0 or 1 in MySQL
- MySQL query with two boolean conditions to extract date based on hour?
- Set different IDs for records with conditions using a single MySQL query
- MongoDB query to $pull / $unset with multiple conditions?
- MySQL If statement with multiple conditions?
- MySQL query to perform selection using AND OR
- MySQL query for text search with LIKE and OR to fetch records
- Multiple COUNT() for multiple conditions in a single MySQL query?
Advertisements