当前位置:网站首页>MySQL case when then function use
MySQL case when then function use
2022-06-12 22:21:00 【Beta Tutu】
One 、 Concept
Function description : Judge the meaning of data display ( similar java if)
grammar :
CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END
Two 、 actual combat
Create and data
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 ' account number ',
`password` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT ' password ',
`status` bit(1) NULL DEFAULT NULL COMMENT ' state :1 normal ,0 Ban ',
`create_date` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0) COMMENT ' Creation time ',
`update_date` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0) COMMENT ' Update time '
)
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');
Case a
demand : Display the account information status in the data sheet in Chinese ( state :1 -> normal , 0-> Ban ).
Realization effect :
sentence :
SELECT id,account,PASSWORD,
( CASE STATUS WHEN 1 THEN ' normal ' WHEN 0 THEN ' Ban ' ELSE NULL END ) AS STATUS
FROM `user`
Case 2
demand : Set the table data according to the creation time (create_date) What sort statistics do ( Similar timeline ).
Realization effect :
sentence :
SELECT create_date,
CONCAT_WS(
',',
CONCAT( ' Create account account:', account ),
CONCAT( ' password password:', PASSWORD ),
CONCAT( ' Account status :', CASE STATUS WHEN 1 THEN ' normal ' WHEN 0 THEN ' Ban ' ELSE NULL END )
) AS line_info
FROM `user` ORDER BY create_date
边栏推荐
- [image denoising] image denoising based on trilateral filter with matlab code
- PCB package download website recommendation and detailed usage
- Pat grade A - 1167 Cartesian tree (30 points) (buildtree + level traversal)
- 【Proteus仿真】简易数码管定时器时钟
- What is the difference between volatile variables and atomic variables?
- be careful! Your Navicat may have been poisoned
- June training (day 12) - linked list
- 在同花顺开户安全么 ,证券开户怎么开户流程
- JVM foundation - > what is STW?
- Jin AI her power | impact tech, she can
猜你喜欢

SQL query list all views in SQL Server 2005 database - SQL query to list all views in an SQL Server 2005 database

设计消息队列存储消息数据的 MySQL 表格

JVM Basics - > how GC determines that an object can be recycled

You can move forward or backward. This function in idea is amazing!

Redis optimization

Preliminary use of jvisualvm

Configuring Dingding notification of SQL audit platform archery

MySQL introduction and installation (I)

A puzzle about + =

反走样/抗锯齿技术
随机推荐
How to abstract a problem into a 0-1 knapsack problem in dynamic programming
My struggle: my years in foreign enterprises (1)
June training (day 10) - bit operation
You can move forward or backward. This function in idea is amazing!
Unity 常用3D数学计算
Is it safe to open an account in tonghuashun? How to open an account for securities
在同花顺开户证券安全吗,买股票怎么网上开户
【LeetCode】53.最大子数组和
Su embedded training day13 - file IO
RAID disk array
JVM Basics - > What are the JVM parameters?
Kotlin collaboration process - flow
证券开户有风险吗?怎么开户安全呢?
The programmer dedicated to promoting VIM has left. Father of vim: I will dedicate version 9.0 to him
ShardingSphere-proxy-5.0.0部署之分表实现(一)
【LeetCode】5. 最长回文子串
PCB package download website recommendation and detailed usage
Hostvars in ansible
Use group_ Dplyr issues when using group_ by(multiple variables)
JS fighting on...