mysql keyword usage summary of case

1. Environment:

mysql 5.7+

navcat premium graphical database management tool

2. Data preparation:

To build the table DDL SQL statements

The CREATE  TABLE `student` ( 
  ` stu_id` int ( 20 ) the NOT  NULL AUTO_INCREMENT, 
  `stu_name` VARCHAR ( 50 ) the DEFAULT  NULL the COMMENT ' student's name ' , 
  ` stu_age` int ( 3 ) the DEFAULT  NULL the COMMENT ' age of the students ' , 
  `stu_sex` VARCHAR ( 6 ) the DEFAULT  NULL the COMMENT ' student sex ' , 
  `add_time`timestamp  the NOT  NULL  the DEFAULT  CURRENT_TIMESTAMP the COMMENT ' Created ' ,
   a PRIMARY  KEY ( `stu_id`) 
) ENGINE = the InnoDB the AUTO_INCREMENT = . 6  the DEFAULT the CHARSET = UTF8 the COMMENT = ' Table students ' ;

Insert data:

BEGIN;
INSERT INTO `student` VALUES (1, 'Drew', 21, '', null), (2, 'Allen', 22, '', null), (3, 'King', 22, '', null);
COMMIT;

Database data:

 

3. Start Highlights :

-- ----------------------------
--      mysql中强大case语句
-- ----------------------------
SELECT
    CASE
        WHEN stu_age > 21 AND stu_age < 23 THEN 1000
        WHEN stu_age >= 23 THEN 2000
        ELSE stu_age 
    END stu_age
FROM student;

The results show:

 The second demo :( attention: do not detect the type of data processed by the data display case statement)

SELECT
    stu_id,
    stu_name,
    CASE
        WHEN stu_age = 21 THEN '二十一岁'
        WHEN stu_age = 22 THEN '二十二岁'
        WHEN stu_age = 23 THEN '二十三岁'
        ELSE stu_age 
    END stu_age,
    stu_sex
FROM student;

结果如下:

 

更加详细请见:https://www.cnblogs.com/vincentbnu/p/9495609.html

 

Guess you like

Origin www.cnblogs.com/superdrew/p/11279631.html