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

Detailed explanation of the problem of CSS class names

The following CSS class names starting with a num...

Several principles for website product design reference

The following analysis is about product design pr...

Tomcat Server Getting Started Super Detailed Tutorial

Table of contents 1. Some concepts of Tomcat –1, ...

What is a MySQL tablespace?

The topic I want to share with you today is: &quo...

Detailed tutorial on installing MySQL offline on CentOS7

1. Delete the original mariadb, otherwise mysql c...

MySQL 5.7.31 64-bit free installation version tutorial diagram

1. Download Download address: https://dev.mysql.c...

MySQL sharding details

1. Business scenario introduction Suppose there i...

Linux five-step build kernel tree

Table of contents 0. The kernel tree that comes w...

The difference between char, varchar and text field types in MySQL

In MySQL, fields of char, varchar, and text types...

HTML table tag tutorial (45): table body tag

The <tbody> tag is used to define the style...

MySQL performance optimization index pushdown

Index condition pushdown (ICP) is introduced in M...

Detailed explanation of mandatory and implicit conversion of types in JavaScript

Table of contents 1. Implicit conversion Conversi...