MySQL Query to Count Rows with Specified Values



To get the count of rows in which two or more specified values appear, let us first create a sample table:

mysql> create table specifiedValuesDemo
   -> (
   -> Value int,
   -> Value2 int,
   -> Value3 int
   -> );
Query OK, 0 rows affected (0.60 sec)

Following is the query to insert some records in the table using insert command:

mysql> insert into specifiedValuesDemo values(10,15,20);
Query OK, 1 row affected (0.17 sec)

mysql> insert into specifiedValuesDemo values(40,10,20);
Query OK, 1 row affected (0.16 sec)

mysql> insert into specifiedValuesDemo values(80,20,1000);
Query OK, 1 row affected (0.12 sec)

Following is the query to display records from the table using select command:

mysql> select *from specifiedValuesDemo;

This will produce the following output

+-------+--------+--------+
| Value | Value2 | Value3 |
+-------+--------+--------+
|    10 |     15 |     20 |
|    40 |     10 |     20 |
|    80 |     20 |   1000 |
+-------+--------+--------+
3 rows in set (0.00 sec)

Let us get the count of rows in which two or more specified values appear:

mysql> select count(*) from specifiedValuesDemo
   -> where 10 in(Value,Value2,Value3) and 20 in(Value,Value2,Value3);

This will produce the following output

+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)
Updated on: 2019-07-30T22:30:25+05:30

77 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements