

- 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
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 Questions & Answers
- 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
- Write a single MySQL query to exclude a record and display NULL value
- Search record with a specific value in MySQL using LIKE or REGEXP?
- How to use three conditions in a single MySQL query with id, name and age of students to fetch record of a student?
- Count and sort rows with a single MySQL query
- MySQL query to fetch record by year
- Priority of AND and OR operator in MySQL select query?
- MySQL query for text search with LIKE and OR to fetch records
- MySQL query to place a specific record on the top
- Delete a specific record from a MySQL table by using AND in WHERE clause
- Can we use WHERE, AND & OR in a single MySQL query?
- MySQL query to find a match and fetch records
- MySQL query to return multiple row records with AND & OR operator
- MySQL select and insert in two tables with a single query
Advertisements