当前位置:网站首页>Mysql case when then函数使用
Mysql case when then函数使用
2022-06-12 22:19:00 【贝塔-突突】
一、概念
功能描述: 判断数据显示代表的含义(类似java if)
语法:
CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END
二、实战
创建和数据
CREATE TABLE `user` (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT 'id',
`account` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '账号',
`password` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码',
`status` bit(1) NULL DEFAULT NULL COMMENT '状态:1正常,0禁用',
`create_date` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`update_date` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '更新时间'
)
INSERT INTO `user` VALUES (1, 'lanys', '123', b'1', '2022-06-10 11:03:34', '2022-06-10 11:03:34');
INSERT INTO `user` VALUES (2, 'Tom', '123', b'1', '2022-06-10 11:03:35', '2022-06-10 11:03:35');
INSERT INTO `user` VALUES (3, 'David', '111', b'0', '2022-06-10 11:03:38', '2022-06-10 11:03:38');
案例一
需求: 将数据表中账号信息状态用中文展示(状态:1 -> 正常, 0-> 禁用)。
实现效果:
语句:
SELECT id,account,PASSWORD,
( CASE STATUS WHEN 1 THEN '正常' WHEN 0 THEN '禁用' ELSE NULL END ) AS STATUS
FROM `user`
案例二
需求: 将表数据根据创建时间(create_date)排序统计做了什么(类似时间线)。
实现效果:
语句:
SELECT create_date,
CONCAT_WS(
',',
CONCAT( '创建账号account:', account ),
CONCAT( '密码password:', PASSWORD ),
CONCAT( '账号状态:', CASE STATUS WHEN 1 THEN '正常' WHEN 0 THEN '禁用' ELSE NULL END )
) AS line_info
FROM `user` ORDER BY create_date
边栏推荐
- [data analysis] data clustering and grouping based on kmeans, including Matlab source code
- Generate the chrysanthemum code of the applet (generate the chrysanthemum code, change the middle logo, change the image size, and add text)
- JVM foundation - > what is STW?
- MySQL introduction and installation (I)
- MySQL体系结构及基础管理(二)
- RAID disk array
- Is it safe to open an account with new bonds? How should novices operate?
- SQL tuning guide notes 16:managing historical optimizer statistics
- 【LeetCode】103. 二叉树的锯齿形层序遍历
- 【LeetCode】数组中第K大的元素
猜你喜欢

2021 rust survey results released: 9354 questionnaires collected
![[data analysis] data clustering and grouping based on kmeans, including Matlab source code](/img/76/deec6cf60c0d02e99ebc3e21d3b8a4.png)
[data analysis] data clustering and grouping based on kmeans, including Matlab source code

回文链表及链表相交问题(和心怡的人相交)你真的会了吗?

MySQL architecture and basic management (II)

Prefix sum and difference

How to perform disaster recovery and recovery for kubernetes cluster? (22)

Redis optimization

Jin AI her power | impact tech, she can

【概率论与数理统计】期末复习抱佛脚:公式总结与简单例题(完结)

JVM foundation - > talk about class loader two parent delegation model
随机推荐
C语言:如何给全局变量起一个别名?
Database daily question --- day 10: combine two tables
Implementation of master-slave replication and master-master replication for MySQL and MariaDB databases
ShardingSphere-proxy-5.0.0部署之分表实现(一)
Ansible foundation and common modules (I)
【数据分析】基于 kmeans实现数据聚类分组含Matlab源码
【LeetCode】300.最长上升子序列
What is the difference between a user thread and a daemon thread?
Su embedded training day13 - file IO
[machine learning] learning notes 01- introduction
【LeetCode】5. 最长回文子串
JVM foundation > G1 garbage collector
Es6+ new content
Preliminary use of jvisualvm
The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables
Xingda easy control modbustcp to profibusdp
NoSQL - redis configuration and optimization (II) high availability, persistence and performance management
MySQL architecture and basic management (II)
JVM Basics - > how to troubleshoot JVM problems in your project
动态规划之如何将问题抽象转化为0-1背包问题(详解利用动态规划求方案数)