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)

Updated on: 30-Jul-2019

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements