表名:aa_company
Column_A Column_B Column_C
ABC 0001 abc@gmail.com
BCD 0002 bcd@gmail.com
CDE 0003 cde@gmail.com
Select Column_A, Column_B, Column_C
FROM aa_company;
如果我想要输出结果再添加一列,我该怎么做?
Column_A Column_B Column_C Column_D
ABC 0001 abc@gmail.com ABC (0001)
BCD 0002 bcd@gmail.com BCD (0002)
CDE 0003 cde@gmail.com CDE (0003)
答案 0 :(得分:0)
CREATE TABLE #TABLE1
([COLUMN_A] VARCHAR(3), [COLUMN_B] VARCHAR(50), [COLUMN_C] VARCHAR(13))
;
INSERT INTO #TABLE1
([COLUMN_A], [COLUMN_B], [COLUMN_C])
VALUES
('ABC', '0001', 'ABC@GMAIL.COM'),
('BCD', '0002', 'BCD@GMAIL.COM'),
('CDE', '0003', 'CDE@GMAIL.COM')
;
使用concat
函数
SELECT COLUMN_A, COLUMN_B, COLUMN_C,CONCAT(COLUMN_A,'('+COLUMN_B+')') AS COLUMN_D FROM #TABLE1
或 使用+运算符
Select Column_A, Column_B, Column_C, Column_A + ' (' + Column_B + ')' COLUMN_D FROM #TABLE1;
答案 1 :(得分:0)
使用concat函数可以获得所需的结果。
Select Column_A, Column_B, Column_C,
concat(column_A,' ('+column_B+')') as Column_D
from aa_company
答案 2 :(得分:0)
Select Column_A, Column_B, Column_C,Column_A + '('+Column_B+')'
FROM aa_company;
这里B列似乎是数字的,所以你需要将它转换为varchar类型,这样查询就像
Select Column_A, Column_B, Column_C,Column_A + '('+CONVERT(varchar(10),Column_B)+')'
FROM aa_company;
为您可以写为
的新列提供标题Select Column_A, Column_B, Column_C,Column_A + '('+CONVERT(varchar(10),Column_B)+')' as Column_D
FROM aa_company;