MySQL query to count the number of times a specific integer appears in a column for its corresponding value in another column


For this, use aggregate function COUNT(). Let us first create a table −

mysql> create table DemoTable650 (Value1 int,Value2 int);
Query OK, 0 rows affected (0.83 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable650 values(100,500);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable650 values(100,500);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable650 values(100,500);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable650 values(100,500);
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable650 values(200,500);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable650;

This will produce the following output −

+--------+--------+
| Value1 | Value2 |
+--------+--------+
| 100    |    500 |
| 100    |    500 |
| 100    |    500 |
| 100    |    500 |
| 200    |    500 |
+--------+--------+
5 rows in set (0.00 sec)

Following is the query to count the number of times a specific integer appears in a column for its corresponding value in another column −

mysql> select Value2,count(Value1) from DemoTable650 where Value1=100 group by Value2;

This will produce the following output −

+--------+---------------+
| Value2 | count(Value1) |
+--------+---------------+
| 500    |             4 |
+--------+---------------+
1 row in set (0.04 sec)

Updated on: 23-Aug-2019

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements