
- 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 perform selection using AND OR
Let us first create a table −
mysql> create table DemoTable ( StudentId int, StudentName varchar(20), StudentSubject varchar(20) ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,'Chris','MongoDB'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(11,'David','MySQL'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(12,'Bob','Java'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(10,'Carol','MySQL'); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable values(10,'Chris','MongoDB'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(14,'David','MySQL'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
Mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+----------------+ | StudentId | StudentName | StudentSubject | +-----------+-------------+----------------+ | 10 | Chris | MongoDB | | 11 | David | MySQL | | 12 | Bob | Java | | 10 | Carol | MySQL | | 10 | Chris | MongoDB | | 14 | David | MySQL | +-----------+-------------+----------------+ 6 rows in set (0.00 sec)
Following is the query to perform selection using AND OR −
mysql> select *from DemoTable where StudentName='David' and StudentSubject='MySQL' OR StudentId=11;
This will produce the following output −
+-----------+-------------+----------------+ | StudentId | StudentName | StudentSubject | +-----------+-------------+----------------+ | 11 | David | MySQL | | 14 | David | MySQL | +-----------+-------------+----------------+ 2 rows in set (0.00 sec)
- Related Articles
- 8085 Program to perform sorting using selection sort
- Program to perform sorting using selection sort in 8085 Microprocessor
- MySQL query to perform sort order on same field
- Field selection within MongoDB query using dot notation?
- MySQL query with OR & AND conditions
- MySQL query to display the column and its values using OR in WHERE statement
- MySQL query to perform delete operation where id is the biggest?
- 8085 Program to perform selection sort in ascending order
- 8085 Program to perform selection sort in descending order
- Implement MySQL query using multiple OR statements. Any optimal alternative?
- Fetch a specific record using a single MySQL query with AND & OR operator
- How to perform SELECT using COUNT in MySQL?
- MySQL query to return multiple row records with AND & OR operator
- Priority of AND and OR operator in MySQL select query?
- DATEADD or DATE_ADD in MySQL query?

Advertisements