【问题标题】:Concatenate string using +=使用 += 连接字符串
【发布时间】:2020-02-27 14:24:26
【问题描述】:

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

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

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

这是我目前的代码

创建或替换 PROCEDURE "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 oracle


【解决方案1】:

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

【讨论】:

  • 这是一个更好的答案,因为我的答案没有删除 rtrim/comma。
【解决方案2】:

我没有你的测试数据,所以很难测试这个,但是试试这样的......

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

【讨论】:

    【解决方案3】:

    CONCAT function

    SELECT CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(liver, kidney_r),kidney_l),heart),liver_domino),lung_r),pancreas),pancreas),liver_split),lung_l),intestine )
    FROM donors
    WHERE id = donNum;
    

    【讨论】:

    • || 更符合人体工程学
    猜你喜欢
    • 1970-01-01
    • 2016-10-25
    • 2010-09-12
    • 2010-12-02
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多