我有三个不同的表,我需要三个表中的相同列,我需要将所有值放入一个输出列而不丢失任何记录。下面是现有的查询建议我做一些修改
SELECT
dbo.dealer_program_details.o_comments,
dbo.Self_Am.o_comments,
dbo.Coop_Payment_Detail.o_comments
FROM
dbo.dealer_program_details
INNER JOIN
dbo.Self_Am
ON
(
dbo.dealer_program_details.DEALER_CODE = dbo.Self_Am.Dealer_Code)
INNER JOIN
dbo.Coop_Payment_Detail
ON
(
dbo.dealer_program_details.DEALER_CODE = dbo.Coop_Payment_Detail.Dealer_Code)
现在我希望所有这三列成为一列
答案 0 :(得分:2)
如果您想将它们放在一列中,请将它们连接在一起:
SELECT (dbo.dealer_program_details.o_comments + dbo.Self_Am.o_comments +
dbo.Coop_Payment_Detail.o_comments
) as OneColumn
FROM dbo.dealer_program_details INNER JOIN
dbo.Self_Am
ON dbo.dealer_program_details.DEALER_CODE = dbo.Self_Am.Dealer_Code INNER JOIN
dbo.Coop_Payment_Detail
ONdbo.dealer_program_details.DEALER_CODE = dbo.Coop_Payment_Detail.Dealer_Code;
在Sybase中,您不必担心NULL
值,因为这些值被视为空字符串。在SQL Server中,如果任何列值为NULL
,则结果为NULL
。
编辑:
您可以使用coalesce()
:
SELECT coalesce(dbo.dealer_program_details.o_comments, dbo.Self_Am.o_comments,
dbo.Coop_Payment_Detail.o_comments
) as OneColumn
但是如果两个(或更多)列有注释,那么你只会保留第一个非NULL值。