Example of using MySQL to count the number of different values ​​in a column

Example of using MySQL to count the number of different values ​​in a column

Preface

The requirement implemented in this article is actually very common. For example, we have a user source table to mark the channel from which the user registered. The table structure is shown below…

Origin is the user source, and its values ​​include iPhone, Android, and Web. Now we need to count the number of users registered by these three channels separately.

Solution 1

SELECT count(*)
FROM user_operation_log
WHERE origin = 'iPhone';
SELECT count(*)
FROM user_operation_log
WHERE origin = 'Android';
SELECT count(*)
FROM user_operation_log
WHERE origin = 'Web';

Use the where statement to count the respective quantities.

This is a bit too much to query. If there are 10 values, you have to write 10 similar statements, which is very troublesome.

Is there a single statement that can do it? So I went to look up some information.

Solution 2

We know that count can be used not only to count the number of rows, but also the number of column values, for example:

Count the number of lines in user_operation_log:

SELECT count(*) FROM user_operation_log

Count the number of values ​​in the origin column that are not NULL:

SELECT count(origin) FROM user_operation_log

So we can use this feature to achieve the above requirements

The first way to write (using count)

SELECT
 count(origin = 'iPhone' OR NULL) AS iPhone,
 count(origin = 'Android' OR NULL) AS Android,
 count(origin = 'Web' OR NULL) AS Web
FROM user_operation_log;

Query results

The second way to write (using sum)

SELECT
 sum(if(origin = 'iPhone', 1, 0)) AS iPhone,
 sum(if(origin = 'Android', 1, 0)) AS Android,
 sum(if(origin = 'Web', 1, 0)) AS Web
FROM user_operation_log;

Query results

The third way to write (rewrite sum)

SELECT
 sum(origin = 'iPhone') AS iPhone,
 sum(origin = 'Android') AS Android,
 sum(origin = 'Web') AS Web
FROM user_operation_log;

Query results

The fourth way to write it (from the answer of Nuggets user Jeff)

SELECT origin,count(*) num FROM user_operation_log GROUP BY origin;

Query results


So far, our needs have been met.

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Detailed examples of converting rows to columns and columns to rows in MySQL
  • Add new fields (columns) to existing tables in the MYSQL database
  • How to add serial numbers to query results in MySQL
  • How to use MySQL to query the number of identical values ​​in a column
  • Return all table names, column names, data type notes of a database in Mysql
  • MySQL adds, modifies, and deletes table columns and constraints, etc.
  • Database implementation of row and column conversion (mysql example)
  • Using dynamic row to column conversion in MySQL stored procedure
  • MySQL column to row conversion, method of merging fields (must read)
  • How to convert column values ​​into columns in mysql
  • MySQL 5.7 generated column usage example analysis

<<:  Implementation of JavaScript downloading linked images and uploading them

>>:  How to implement Mysql scheduled task backup data under Linux

Recommend

Vue implements paging function

This article example shares the specific code of ...

Three notification bar scrolling effects implemented with pure CSS

Preface The notification bar component is a relat...

Nginx configuration 80 port access 8080 and project name address method analysis

Tomcat accesses the project, usually ip + port + ...

80 lines of code to write a Webpack plugin and publish it to npm

1. Introduction I have been studying the principl...

Summary of Docker configuration container location and tips

Tips for using Docker 1. Clean up all stopped doc...

Solution to input cursor misalignment in Chrome, Firefox, and IE

Detailed explanation of the misplacement of the in...

Differences between this keyword in NodeJS and browsers

Preface Anyone who has learned JavaScript must be...

Detailed explanation of JDBC database link and related method encapsulation

Detailed explanation of JDBC database link and re...

Detailed process of NTP server configuration under Linux

Table of contents 1. Environment Configuration 1....

Mac VMware Fusion CentOS7 configuration static IP tutorial diagram

Table of contents Install CentOS7 Configuring Sta...

VPS builds offline download server (post-network disk era)

motivation Due to learning needs, I purchased a v...

Velocity.js implements page scrolling switching effect

Today I will introduce a small Javascript animati...

How to install Zookeeper service on Linux system

1. Create the /usr/local/services/zookeeper folde...

MySQL 5.7.17 installation and configuration method graphic tutorial (windows10)

MySQL 5.7.17 installation and configuration metho...