Get the App
SLTechnology News&Howtos  ›  Database  › 

Detailed explanation of mysql case when group by examples

Shulou Source: shulou.com Published: 2022-06-01 03:30:03 11月16日 Update

A php switch case-like statement in mysql.

Select xx field, case field when condition 1 then value 1when condition 2 then value 2else other values END alias from table name

The following is an example of grouping summation:

Select sum (redpackmoney) as stota,ischeck from t_redpackgroup 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_redpackgroup by isCheck

Replace-1,0,1 in checkinfo with checkfalse, uncheck, checktrue (CASE WHEN performs string replacement processing)

The above is a simple example of the use of case when statements in mysql.

Tags: Fields conditions examples statements aliases characters strings that is grouping processing instances Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Technology NVidia Shulou Tech Info Xiaomi