

- 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
How to select a row where one of several columns equals a certain value in MySQL?
For this, you can use multiple OR. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(10), LastName varchar(10), Age int, CountryName varchar(10) ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName,LastName,Age,CountryName) values('John','Smith',21,'US'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName,LastName,Age,CountryName) values('Carol','Taylor',22,'AUS'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FirstName,LastName,Age,CountryName) values('David','Miller',19,'UK'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+-----------+----------+------+-------------+ | Id | FirstName | LastName | Age | CountryName | +----+-----------+----------+------+-------------+ | 1 | John | Smith | 21 | US | | 2 | Carol | Taylor | 22 | AUS | | 3 | David | Miller | 19 | UK | +----+-----------+----------+------+-------------+ 3 rows in set (0.00 sec)
Following is the query to select a row where one of several columns equals a certain value −
mysql> select *from DemoTable where FirstName="Carol" OR Age=22 OR CountryName="AUS";
Output
+----+-----------+----------+------+-------------+ | Id | FirstName | LastName | Age | CountryName | +----+-----------+----------+------+-------------+ | 2 | Carol | Taylor | 22 | AUS | +----+-----------+----------+------+-------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- SELECT where row value contains string in MySQL?
- SELECT a row by subtracting dates in WHERE in MySQL?
- MySQL query to select column where value = one or value = two, value = three, etc?
- How to select data in MySQL where a field has a minimum value?
- How to select all the records except a row with certain id from a MySQL table?
- SELECT * WHERE var == [one of many alternatives] in MySQL?
- DELETE from MySQL table WHERE column equals value AND column2 equals value2?
- Comparing two columns in a single MySQL query to get one row?
- MySQL Select Rows where two columns do not have the same value?
- MySQL query to select one specific row and another random row?
- Select a random row in MySQL
- How to select all columns except one in a Pandas DataFrame?
- MySQL LIMIT to select a single row
- How to select most recent date out of a set of several possible timestamps in MySQL?
- How do you select from MySQL where last value in a string = x?
Advertisements