sql - 使用 += 连接字符串

标签 sql oracle

我正在尝试在 sql 中连接一些字符串。我想做的是类似

string organType = null;
if (liver!=null)  { organType += "LI, "; }
if (kidney !=null) { organType += "KI, "; }
if (intestine != null) { organType += "Intestine"; } 
...

最终结果应该是organType = LI, KI, Intestine;

到目前为止,这是我的代码

创建或替换过程“insertDonInfo”(donNum IN NUMBER,offerDate IN DATE)

organType varchar2(100);
BEGIN

  select case when liver is not null then 'LI' 
              when kidney_r is not null then 'KR'
              when kidney_l is not null then 'KL' 
              when heart is not null then 'HE'
              when liver_domino is not null then 'LI-Dom'
              when lung_r is not null then 'LungR'
              when pancreas is not null then 'PA'
              when liver_split is not null then 'Lsplit'
              when lung_l is not null then 'LungL'
              when intestine is not null then 'Intestine' 
         end                         
from donors
where id = donNum;

...

------------------------更新--------------------

如何在 SQL 中将 organType 连接为 organType=LI, KR, KL, HE, ...;

最佳答案

sql 没有 += 运算符。您必须逐列检查并连接。试了一下您的数据结构。

create table so_test (id number primary key, don_name varchar2(100), liver varchar2(1), heart varchar2(1), kidney_r varchar2(1));

insert into so_test (id, don_name, liver, heart, kidney_r) values (1, 'John','Y',NULL,'Y');
insert into so_test (id, don_name, liver, heart, kidney_r) values (2, 'Kathy',NULL,'Y','Y');

SELECT 
  don_name,
  RTRIM(
    CASE WHEN liver IS NOT NULL THEN 'LI, ' ELSE NULL END ||
    CASE WHEN heart IS NOT NULL THEN 'HE, ' ELSE NULL END ||
    CASE WHEN kidney_r IS NOT NULL THEN 'KR, ' ELSE NULL END
  ,', ') as organs
  FROM so_test;

返回

John    LI, KR
Kathy   HE, KR

关于sql - 使用 += 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58656678/

相关文章:

mysql - 根据列选择不同的行组合

oracle - 将 micronaut-data 与 JDBC 结合使用时如何获取数据库序列值

php - SQL - 返回最大列值

用于更改所有存储过程中的所有表引用的 SQL 脚本

sql - 如何将map传入spark中的UDF

sql - Oracle SQL 比较包含数字的字符串,从 0(零)开始

sql - 每辆车相对有 Count(For that Day), Count for last 10 days 和 Count of last 20 days

mysql - 插入数据库时​​表情符号替换为问号

sql - 如何选择与同一个表中的相应行不匹配的行?

java - 如何在更新 Oracle 中的列 Clob 时提高性能?