在SQL中连接两行 [英] Concatenate two rows in SQL

查看:141
本文介绍了在SQL中连接两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ID&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP EMAIL

_____________________________________________

EMAIL01&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP xxx.xxx

EMAIL01A                       &xbsp @ xxx.com





请帮我合并这两行来创建像这样的电子邮件:xxx.xxx@xxx.com作为单行。

ID                                     EMAIL
_____________________________________________
EMAIL01                         xxx.xxx
EMAIL01A                      @xxx.com


Please help me to merge these two rows to create an email like this: xxx.xxx@xxx.com as a single row.

推荐答案

问题中没有足够的信息来理解为什么数据库表会是以这种方式设计以及表中可能需要以类似方式选择的其他行。不过,下面是一个Microsoft SQL Server SELECT 语句,它将以您在问题中指定的方式连接这两行以创建电子邮件地址。



There is not enough information in the question to understand why a database table would be designed this way and what other rows are in the table that might need to be selected in a similar manner. Nevertheless, below is a Microsoft SQL Server SELECT statement that will concatenate the two rows in the manner that you specified in your question to create an email address.

Select (Select RTRIM(EMAIL) from table_1 where ID=''EMAIL01'') + (Select RTRIM(EMAIL) from table_1 where ID=''EMAIL01A'')





注意:在SQL Server Express 2012上使用Microsoft SQL Server Management Studio进行测试


选择(从urTable中选择ltrim(rtrim(Email)),其中id =''Email01'')+(从urTable中选择ltrim(rtrim(Email)),其中id =''Email01A'')

使用此查询
select (select ltrim(rtrim(Email)) from urTable where id=''Email01'') + (select ltrim(rtrim(Email)) from urTable where id=''Email01A'')
use this query


假设您的表格为THE_TABLE:





Suppose your table is "THE_TABLE":


SELECT T1.ID, T1.EMAIL + T2.EMAIL FROM THE_TABLE T1 
  JOIN THE_TABLE T2 
     ON T2.ID = (T1.ID + 'A')





那应该有用;)



That should work ;)


这篇关于在SQL中连接两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆