当前位置:网站首页>mysql中case when返回多个字段处理方案
mysql中case when返回多个字段处理方案
2022-07-27 02:30:00 【卖柴火的小伙子】
问题说明
一般情况下case when结构,返回的字段只能支持一个,但是会有很多业务场景中需要返回一个以上字段信息.这种问题有两个处理方案,一个是将case when结构进行改造,支持返回多个字段;另一种就是保持case when返回字段个数不变(只返回一个字段),修改业务逻辑,在service中按照case when返回一个字段进行处理.本文主要介绍第一种处理方案.下面结合一个业务场景说明如何进行处理.
业务问题场景描述
现在有一个求职招聘的平台,招聘者会发送简历发送的邀请通知,作为求职端有三种返回状态,1.接收简历发送邀请.2.拒绝简历发送邀请,填写拒绝原因(1.距离太远;2.已找到工作;3.其他);3.收到信息不做处理,即不接受也不拒绝.需要一个sql查询出需要返回两个字段:是否接受简历发送邀请,三种状态:1.接收,2.拒绝,3.不做处理;拒绝状态:0.无拒绝处理;1.距离太远;2.已找到工作;3.其他.只有当求职者拒绝简历发送时拒绝状态才会从1-3中进行取值,接收简历发送邀请或是不做处理时此状态需要返回0.
业务表简要说明:job_send_post为求职者简历投递记录表,只要是求职者发送过简历都会从此表中添加记录;job_news为求职者发送给招聘者的消息记录表.
处理方式
case when只允许返回一个字段,原来的不支持就只能自己"新增字段",所以可以使用concat函数进行拼接,此种拼接可以支持拼接多个字段.总结的写法如下:
CASE WHEN
条件
THEN CONCAT(字段1,'+',字段2,'+',字段3......)
ELSE CONCAT(字段1,'+',字段2,'+',字段3......)
END
-- 查询招聘端消息记录中求职端是否发送过拒绝简历消息以及拒绝类型,如果没有发送则简历邀请状态为3,拒绝状态为0.如果有则简历邀请状态为2,拒绝原因使用查询出来的refuse_type拒绝类型
IFNULL((SELECT CONCAT(2,"+",refuse_type) FROM job_news WHERE user_id='rxkyogtktutm4pbsi18e' ),CONCAT(3,'+',0))
END) receive_state
这个场景中就是需要将是否接受邀请字段与拒绝状态字段进行拼接,注意字段中间需要使用一个特殊字符进行拼接,方便service层进行截取.具体执行如下:
SELECT
(CASE WHEN
-- 判断user_id为rxkyogtktutm4pbsi18e的求职者是否给招聘端投递过简历记录,如果投递过则说明接受过招聘端的简历邀请,简历邀请状态为1,拒绝状态为0
IF((SELECT 1 FROM job_send_post WHERE user_id='rxkyogtktutm4pbsi18e' LIMIT 1)=1,TRUE,FALSE)
THEN
CONCAT(1,'+',0)
ELSE
-- 查询招聘端消息记录中求职端是否发送过拒绝简历消息以及拒绝类型,如果没有发送则简历邀请状态为3,拒绝状态为0.如果有则简历邀请状态为2,拒绝原因使用查询出来的refuse_type拒绝类型
IFNULL((SELECT CONCAT(2,"+",refuse_type) FROM job_news WHERE user_id='rxkyogtktutm4pbsi18e' ),CONCAT(3,'+',0))
END) receive_state
以上是处理问题过程,如有帮助,欢迎点赞或留言!
边栏推荐
- ZJCTF_ login
- Is it safe for tongdaxin to open an account
- 222. 完全二叉树的节点个数
- Daffodils (day 78)
- Programming implementation of eight queens
- It's confirmed that the registration of soft exam in the second half of 2022 will start in August
- [untitled]
- Is Jiufang intelligent investment a regular company? Talk about Jiufang intelligent investment
- 【无标题】JDBC连接数据库读超时
- DNS record type and explanation of related terms
猜你喜欢

复盘:图像有哪些基本属性?关于图像的知识你知道哪些?图像的参数有哪些
![[Android synopsis] kotlin multithreaded programming (I)](/img/04/4349bacbd401868d73a3b05d018b66.png)
[Android synopsis] kotlin multithreaded programming (I)

Deployment of ruoyi's environment and operation of the system
![Machine learning [Matplotlib]](/img/d1/31ec2f96ca96465c6863798c7db179.jpg)
Machine learning [Matplotlib]

Plato farm is expected to further expand its ecosystem through elephant swap

222. 完全二叉树的节点个数

Network security / penetration testing tool awvs14.9 download / tutorial / installation tutorial

Solution to Chinese garbled code in console header after idea connects to database to query data

Learning and understanding of four special data types of redis

关于使用hyperbeach出现/bin/sh: 1: packr2: not found的解决方案
随机推荐
Day 28 of leetcode
GetObject call timing of factorybean
Will this flinkcdc monitor all tables in the database? Or the designated table? I look at the background log. It monitors all tables. If it monitors
Number of square arrays (day 81)
使用redis c库,异步内存泄露的问题
Process analysis of object creation
Contour detection based on OpenCV (2)
Ming min investment Qiu Huiming: behind the long-term excellence and excess, the test is the team's investment and research ability and the integrity of strategy
Leetcode- > 2-point search and clock in (3)
NLP hotspots from ACL 2022 onsite experience
URDF_ Xcaro
代码回滚,你真的理解吗?
MySQL Chinese failure
Design method and test method of APP interface use case
Permutation and binary (Ji, DA) (day 84)
04. Detailed steps for installing the simulated browser chromedriver in Google browser
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
【安卓小叙】Kotlin多线程编程(一)
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
Vector to SVG method