MySQL case when usage example analysis

MySQL case when usage example analysis

First we create the database table:

CREATE TABLE `t_demo` (
 `id` int(32) NOT NULL,
 `name` varchar(255) DEFAULT NULL,
 `age` int(2) DEFAULT NULL,
 `num` int(3) DEFAULT NULL,
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

Insert data:

INSERT INTO `t_demo` VALUES ('1', '张三', '21', '69');
INSERT INTO `t_demo` VALUES ('2', 'Li Si', '22', '98');
INSERT INTO `t_demo` VALUES ('3', '王五', '20', '54');
INSERT INTO `t_demo` VALUES ('4', '赵甜', '22', '80');

MySQL case when usage

SELECT
  *,
CASE
  WHEN t.num >= 85 THEN
    'excellent' 
  WHEN t.num < 90 AND t.num>= 60 THEN
    'generally'
  ELSE
    'Failure'
  END AS level
FROM
  t_demo t;

Query structure:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of using judgment statements (IF ELSE/CASE WHEN) in SQL Server
  • MySQL cleverly uses sum, case and when to optimize statistical queries
  • Solve the problem of mybatis case when error
  • Oracle uses decode function or CASE-WHEN to implement custom sorting
  • This article will show you how to use SQL CASE WHEN in detail

<<:  Nginx improves access speed based on gzip compression

>>:  Vue implements start time and end time range query

Recommend

The whole process of realizing website internationalization using Vite2 and Vue3

Table of contents Preface Install vue-i18n Config...

In-depth understanding of slot-scope in Vue (suitable for beginners)

There are already many articles about slot-scope ...

Linux server quick uninstall and install node environment (easy to get started)

1. Uninstall npm first sudo npm uninstall npm -g ...

A brief discussion on two methods to solve space-evenly compatibility issues

Since its launch in 2009, flex has been supported...

MySQL view introduction and basic operation tutorial

Preface View is a very useful database object in ...

JS implements dragging the progress bar to change the transparency of elements

What I want to share today is to use native JS to...

25 CSS frameworks, tools, software and templates shared

Sprite Cow download CSS Lint download Prefixr dow...

How to install MySQL 5.7.28 binary mode under CentOS 7.4

Linux system version: CentOS7.4 MySQL version: 5....

JS implements simple calendar effect

This article shares the specific code of JS to ac...

MySQL 5.7.24 installation and configuration graphic tutorial

This article shares the installation and configur...

Detailed tutorial on installing pxc cluster with docker

Table of contents Preface Preliminary preparation...

How to find out uncommitted transaction information in MySQL

A while ago, I wrote a blog post titled "Can...