简体   繁体   English

concat_ws 基于 SQL 查询中列的值的字符串

[英]concat_ws a string based on the value of the column in the SQL query

I am trying to concatenate values from multiple fields using concat_ws .我正在尝试使用concat_ws连接来自多个字段的值。 One of the fields ( is_logged ) contains only the values 0 or 1 .其中一个字段 ( is_logged ) 仅包含值01 I want to concatenate yes if the value of is_logged field is 1 and no otherwise.如果is_logged字段的值为1 ,我想连接yes ,否则连接no

Eg - concat_ws('', month,'-', year, ',Logged-', is_logged) info例如 - concat_ws('', month,'-', year, ',Logged-', is_logged) info

Current output - Dec - 2020,Logged-1当前 output - Dec - 2020,Logged-1

Expected output - Dec - 2020,Logged-Yes预计 output - Dec - 2020,Logged-Yes

How can this be achieved?如何做到这一点? Thanks!谢谢!

can be different in different dbms but you can do this:在不同的 dbms 中可能会有所不同,但您可以这样做:

select concat_ws('',month,'-',year,',Logged-',case is_logged when 1 then 'yes' else 'no' end) info
from yourtable

I got this working based on this answer -我根据这个答案得到了这个工作 -

concat_ws('', month,'-', year, ',Logged-',IFNULL(ELT(FIELD(is_logged,1),'Yes'),'No')) info

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM