当前位置:网站首页>Case when in MySQL returns multiple field processing schemes
Case when in MySQL returns multiple field processing schemes
2022-07-27 04:00:00 【Firewood boy】
Problem specification
In general case when structure , Only one field can be returned , But there are many business scenarios that need to return more than one field . There are two solutions to this problem , One is to case when The structure is transformed , Support to return multiple fields ; The other is to keep case when The number of return fields remains unchanged ( Only one field is returned ), Modify business logic , stay service According to the in case when Return a field for processing . This paper mainly introduces the first treatment scheme . Here is a business scenario to illustrate how to handle .
Business problem scenario description
Now there is a platform for job recruitment , The recruiter will send the invitation notice sent by resume , As a job seeker, there are three return States ,1. Receive resume and send invitation .2. Reject the invitation to send resume , Fill in the rejection reason (1. Too far away ;2. Found a job ;3. other );3. Do not process the received information , Neither accept nor refuse . Need one sql The query needs to return two fields : Whether to accept the invitation to send resume , Three states :1. receive ,2. Refuse ,3. Don't deal with it ; Rejection status :0. No rejection processing ;1. Too far away ;2. Found a job ;3. other . Only when the applicant refuses to send the resume, the rejection status will change from 1-3 Value in , When receiving resume and sending invitation or not processing, this status needs to be returned 0.
Brief description of business table :job_send_post Send the resume record form for the job seeker , As long as the applicant has sent a resume, records will be added to this form ;job_news Record the information sent to the recruiter for the job seeker .
Processing mode
case when Only one field is allowed to be returned , If you don't support it, you can only do it yourself " New fields ", So you can use concat Function , This splicing can support splicing multiple fields . The summary is written as follows :
CASE WHEN
Conditions
THEN CONCAT( Field 1,'+', Field 2,'+', Field 3......)
ELSE CONCAT( Field 1,'+', Field 2,'+', Field 3......)
END
-- Query whether the job search end has sent a rejection resume message and the rejection type in the recruitment end message record , If it is not sent, the resume invitation status is 3, Reject status is 0. If yes, the resume invitation status is 2, The reason for rejection is found by using refuse_type Reject type
IFNULL((SELECT CONCAT(2,"+",refuse_type) FROM job_news WHERE user_id='rxkyogtktutm4pbsi18e' ),CONCAT(3,'+',0))
END) receive_state
In this scenario, it is necessary to splice the accept invitation field and the reject status field , Note that a special character needs to be used in the middle of the field for splicing , convenient service Layer to intercept . The specific implementation is as follows :
SELECT
(CASE WHEN
-- Judge user_id by rxkyogtktutm4pbsi18e Has the applicant submitted resume records to the recruitment end , If it has been delivered, it means that it has accepted the resume invitation of the recruitment end , Resume invitation status is 1, Reject status is 0
IF((SELECT 1 FROM job_send_post WHERE user_id='rxkyogtktutm4pbsi18e' LIMIT 1)=1,TRUE,FALSE)
THEN
CONCAT(1,'+',0)
ELSE
-- Query whether the job search end has sent a rejection resume message and the rejection type in the recruitment end message record , If it is not sent, the resume invitation status is 3, Reject status is 0. If yes, the resume invitation status is 2, The reason for rejection is found by using refuse_type Reject type
IFNULL((SELECT CONCAT(2,"+",refuse_type) FROM job_news WHERE user_id='rxkyogtktutm4pbsi18e' ),CONCAT(3,'+',0))
END) receive_state
The above is the process of dealing with problems , If help , Welcome to like or leave a message !
边栏推荐
- C# 使用SqlSugar Updateable系统报错无效数字,如何解决?求指导!
- The job created by flinksqlclient will disappear after the restart of Flink. Is there any way?
- Introduction to redis
- Leetcode- > dichotomy (III)
- Prime factorization -- C (GCC) -- PTA
- A. YES or YES?
- Function pointer and callback function
- 开机启动流程及营救模式
- Is Jiufang intelligent investment a regular company? Talk about Jiufang intelligent investment
- Food chain (day 79)
猜你喜欢

Source code analysis of openfeign

Application, addition and deletion of B-tree

flask_ Reqparse parser inheritance in restful

深圳家具展首日,金可儿展位三大看点全解锁!

Is Jiufang intelligent investment a regular company? Talk about Jiufang intelligent investment

Cocos game practice-04-collision detection and NPC rendering
![[tree chain dissection] template question](/img/6b/7ec6f36d5f2373aee163c2cb766b29.png)
[tree chain dissection] template question

Message queue learning -- Concepts

SkyWalking分布式系统应用程序性能监控工具-中

The fifth strong network cup national network security challenge Title reappearance (with title attachment, detailed explanation)
随机推荐
NLP hotspots from ACL 2022 onsite experience
Binary tree (day 82)
Redis(九) - Redis之分布式锁
03.获取网页源代码
[untitled]
Application of one-dimensional array
Csu18m91 is used as the master controller of the intelligent scale scheme
Feitengtengrui d2000 won the "top ten hard core technologies" award of Digital China
Redis source code learning (33), command execution process
03. Get the web page source code
Worthington papain dissociation system solution
Debug mode in pycharm for detailed debugging
Day 28 of leetcode
LeetCode 第二十七天
The job created by flinksqlclient will disappear after the restart of Flink. Is there any way?
第五届强网杯全国网络安全挑战赛 题目复现(有题目附件,详解)
C # using sqlsugar updatable system to report invalid numbers, how to solve it? Ask for guidance!
Data analysis and disassembly method of banyan tree in Bairong
Chapter 4 decision tree and random forest
Connman introduction