Sql将两列合并为一个变量

时间:2014-02-26 07:12:57

标签: sql oracle

我有一个sql命令

SELECT A.*,
       K1.AD CONTACTAD,
       K1.SYD CONTACTSYD,
       KR.AD KURUM,
       K2.AD SUPERVISORAD,
       K2.SYD  SUPERVISORSOYAD,
       K3.AD REQUIRERAD,
       K3.SYD REQUIRERSOYAD
FROM  
       TD_ACTIVITY A,
       KK_KS K1,
       KK_KS K2,
       KK_KS K3,
       KK_KR KR
WHERE  A.INS_CONTACT_PERSON = K1.ID
   AND A.SUPERVISOR =  K2.ID
   AND A.REQUIRERID = K3.ID 
   AND A.INSTITUTIONID = KR.ID

我想将CONTACTADCONTACTSOYAD合并到一个变量中,例如CONTACT。我该怎么做?

6 个答案:

答案 0 :(得分:5)

使用||在oracle中连接

SELECT A.*,
       K1.AD || K1.SYD as CONTACT,
       KR.AD KURUM,
       K2.AD SUPERVISORAD,
       K2.SYD  SUPERVISORSOYAD,
       K3.AD REQUIRERAD,
       K3.SYD REQUIRERSOYAD
FROM  
       TD_ACTIVITY A,
       KK_KS K1,
       KK_KS K2,
       KK_KS K3,
       KK_KR KR
WHERE  A.INS_CONTACT_PERSON = K1.ID
   AND A.SUPERVISOR =  K2.ID
   AND A.REQUIRERID = K3.ID 
   AND A.INSTITUTIONID = KR.ID

答案 1 :(得分:3)

试试这个,

SELECT A.*,
       concat(k1.ad, k1.syd) concat
       kr.ad kurum,
       k2.ad supervisorad,
       k2.syd supervisorsoyad,
       k3.ad requirerad,
       k3.syd requirersoyad
FROM   td_activity A,
       kk_ks k1,
       kk_ks k2,
       kk_ks k3,
       kk_kr kr
WHERE  A.ins_contact_person = k1.ID
AND    A.supervisor         = k2.ID
AND    A.requirerid         = k3.ID
AND    A.institutionid      = kr.ID;

请参阅here以了解有关Oracle中CONCAT的更多信息。

答案 2 :(得分:3)

使用Oracle concatenation operator

SELECT A.*,
       K1.AD || ' ' || K1.SYD CONTACT
       KR.AD KURUM,
       K2.AD SUPERVISORAD,
       K2.SYD SUPERVISORSOYAD,
       K3.AD REQUIRERAD,
       K3.SYD REQUIRERSOYAD 
  FROM TD_ACTIVITY A,
       KK_KS K1,
       KK_KS K2,
       KK_KS K3,
       KK_KR KR 
 WHERE A.INS_CONTACT_PERSON = K1.ID 
       AND A.SUPERVISOR =  K2.ID 
       AND A.REQUIRERID = K3.ID 
       AND A.INSTITUTIONID = KR.ID

答案 3 :(得分:2)

SELECT K1.AD || columnName "CONTACT"

此处columnName是别名为CONTACTSOYAD的列的名称。

您无法在相同的SELECT列表中操作别名,因此下面会出错

SELECT CONTACTAD ||  CONTACTSOYAD "CONTACT" , ...rest of columns...
FROM
..rest of query..

答案 4 :(得分:1)

请尝试:

(K1.AD||K1.SYD) AS CONTACT

答案 5 :(得分:1)

你可以试试这个:

select CONTACTAD || CONTACTSOYAD from emp;

||是Oracle中的连接运算符

希望这会有所帮助..