当前位置:网站首页>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
边栏推荐
- JVM Basics - > how to troubleshoot JVM problems in your project
- Producer consumer model under multithreading model
- List of open source alternative projects of world famous Cloud Service SaaS companies
- 设计消息队列存储消息数据的 MySQL 表格
- Is it safe to open an account in tonghuashun? How to open an account
- The 2023 campus recruitment officially opened! Oceanbase would like to make an interview with you this spring
- 大学期间零基础如何开展编程学习
- Hostvars in ansible
- 3.5 测试类的setup和teardown
- [image denoising] image denoising based on trilateral filter with matlab code
猜你喜欢

JVM Basics - > how to troubleshoot JVM problems in your project

Dolphin-2.0.3 cluster deployment document

MySQL architecture and basic management (II)

Use group_ Dplyr issues when using group_ by(multiple variables)

RAID disk array

MySQL体系结构及基础管理(二)

NoSQL - redis configuration and optimization (II) high availability, persistence and performance management

Ansible基础和常用模块(一)

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

How to perform disaster recovery and recovery for kubernetes cluster? (22)
随机推荐
Step by step evolution of restful API version Frankel
What is the difference between volatile variables and atomic variables?
JVM Basics - > What are the thread shared areas in the JVM
Why is pain rating important?
Ansible playbook and variable (II)
[sword finger offer simple] sword finger offer 24 Reverse linked list
List of open source alternative projects of world famous Cloud Service SaaS companies
Economist focuses on WTO MC12: digital economy may become an important issue
Pat grade A - 1167 Cartesian tree (30 points) (buildtree + level traversal)
Mysql concat_ws、concat函数使用
Hostvars in ansible
Ansible基础和常用模块(一)
Xingda easy control modbustcp to profibusdp
JVM foundation - > What garbage collectors does the JVM have?
【LeetCode】102. 二叉树的层序遍历
"Oracle database parallel execution" technical white paper reading notes
Flutter series part: detailed explanation of GridView layout commonly used in flutter
2021 rust survey results released: 9354 questionnaires collected
Research Report on market supply and demand and strategy of China's digital camera lens industry
How to specify your webpage's language so Google Chrome doesn't offer to translate it