
- 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
Fetch a specific record using a single MySQL query with AND & OR operator
Let us first create a table −
mysql> create table DemoTable2015 -> ( -> StudentId int, -> StudentName varchar(20), -> StudentCountryName varchar(20) -> ); Query OK, 0 rows affected (1.20 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2015 values(1,'Chris','US'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable2015 values(2,'Bob','UK'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2015 values(3,'David','AUS'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2015 values(4,'Robert','US'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2015;
This will produce the following output −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 1 | Chris | US | | 2 | Bob | UK | | 3 | David | AUS | | 4 | Robert | US | +-----------+-------------+--------------------+ 4 rows in set (0.00 sec)
Here is the query to fetch a specific record with a single query −
mysql> select *from DemoTable2015 -> where StudentName='David' OR StudentCountryName='UK' and StudentId=3;
This will produce the following output −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 3 | David | AUS | +-----------+-------------+--------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to find the previous and next record using a single query in MySQL?
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- How to use three conditions in a single MySQL query with id, name and age of students to fetch record of a student?
- Search record with a specific value in MySQL using LIKE or REGEXP?
- Write a single MySQL query to exclude a record and display NULL value
- MySQL query to fetch record by year
- MySQL query to place a specific record on the top
- Count and sort rows with a single MySQL query
- MySQL query for text search with LIKE and OR to fetch records
- MySQL query to return multiple row records with AND & OR operator
- Can we use WHERE, AND & OR in a single MySQL query?
- Priority of AND and OR operator in MySQL select query?
- Delete a specific record from a MySQL table by using AND in WHERE clause
- Use LIKE % to fetch multiple values in a single MySQL query
- Using MySQL keywords in a query surrounded with single quotes?

Advertisements