MySQL filtering by multiple columns?



To perform filtering by multiple columns, use where clause along with OR. Let us first create a table −

mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(10),    Score int    ); Query OK, 0 rows affected (0.28 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Name,Score) values('John',80); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Name,Score) values('John',90); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Name,Score) values('Carol',89); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Name,Score) values('John',86); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Name,Score) values('Carol',98); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Name,Score) values('David',99); Query OK, 1 row affected (0.06 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+-------+-------+ | Id | Name | Score | +----+-------+-------+ | 1 | John | 80 | | 2 | John | 90 | | 3 | Carol | 89 | | 4 | John | 86 | | 5 | Carol | 98 | | 6 | David | 99 | +----+-------+-------+ 6 rows in set (0.00 sec)

Following is the query to perform filtering by multiple columns −

mysql> select *from DemoTable where (Name,Score)=('John', 90) or (Name,Score)=('Carol', 98);

This will produce the following output −

+----+-------+-------+ | Id | Name | Score | +----+-------+-------+ | 2 | John | 90 | | 5 | Carol | 98 | +----+-------+-------+ 2 rows in set (0.00 sec)
Updated on: 2019-07-30T22:30:26+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements