- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements