MySQL case when group by example

MySQL case when group by example

A mysql-like php switch case statement.

  1. select xx field,
  2. Case Field
  3. when condition 1 then value 1
  4. when condition 2 then value 2
  5. else other value END alias
  6. from table name;

Here is an example of group summation:

select sum(redpackmoney) as stota,ischeck 
from t_redpack
group by isCheck

Use case when :

select sum(redpackmoney) as stota,
(CASE isCheck WHEN '1' THEN 'checktrue' WHEN '0' THEN 'uncheck' WHEN '-1' THEN 'checkfalse' ELSE 'unknow' END) as checkinfo 
from t_redpack
group by isCheck

In checkinfo, -1, 0, 1 are replaced with checkfalse, uncheck, checktrue (CASE WHEN performs string replacement processing)

The above is a simple example of using the case when statement in MySQL.

You may also be interested in:
  • MySQL case when usage example analysis
  • Detailed example of using case statement in MySQL stored procedure
  • Example of using CASE WHEN in MySQL sorting
  • In-depth analysis of the Identifier Case Sensitivity problem in MySQL
  • Detailed explanation of Mysql case then usage
  • mysql update case update field value is not fixed operation

<<:  Vue implements simple data two-way binding

>>:  How to start tomcat using jsvc (run as a normal user)

Recommend

A QQ chat room based on vue.js

Table of contents Introduction The following is a...

How to install MySQL 8.0 in Docker

Environment: MacOS_Cetalina_10.15.1, Mysql8.0.18,...

win10 docker-toolsbox tutorial on building a php development environment

Download image docker pull mysql:5.7 docker pull ...

Ubuntu 20.04 Chinese input method installation steps

This article installs Google Input Method. In fac...

JavaScript data visualization: ECharts map making

Table of contents Overview Precautions 1. Usage 2...

Vue implements the full selection function

This article example shares the specific code of ...

How to set utf-8 encoding in mysql database

Modify /etc/my.cnf or /etc/mysql/my.cnf file [cli...

Implementation of MySQL master-slave status check

1. Check the synchronization status of A and B da...

Measured image HTTP request

Please open the test page in a mainstream browser...

How to modify the group to which a user belongs in Linux

Modify the group to which a user belongs in Linux...

Mysql experiment: using explain to analyze the trend of indexes

Overview Indexing is a skill that must be mastere...

How to optimize logic judgment code in JavaScript

Preface The logical judgment statements we use in...