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

CSS draw a lollipop example code

Background: Make a little progress every day, acc...

Docker large-scale project containerization transformation

Virtualization and containerization are two inevi...

Implementation steps for docker deployment of springboot and vue projects

Table of contents A. Docker deployment of springb...

Summary of Common Commands for Getting Started with MySQL Database Basics

This article uses examples to describe the common...

JavaScript Prototype Details

Table of contents 1. Overview 1.1 What is a proto...

What is the function and writing order of the a tag pseudo class

The role of the a tag pseudo-class: ":link&qu...

Summary of solutions for MySQL not supporting group by

I downloaded and installed the latest version of ...

MySQL's method of dealing with duplicate data (preventing and deleting)

Some MySQL tables may contain duplicate records. ...

How to configure pseudo-static and client-adaptive Nginx

The backend uses the thinkphp3.2.3 framework. If ...

A very detailed tutorial on installing rocketmq under Docker Desktop

Install Docker Desktop Download address: Docker D...

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

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

How to redirect URL using nginx rewrite

I often need to change nginx configuration at wor...

A practical record of restoring a MySQL Slave library

Description of the situation: Today, I logged int...