SQL Server - 如何合并具有相同列名的2个不同表中的2个不同值

时间:2017-11-07 01:04:07

标签: sql sql-server sql-update

我想只显示它们具有不同值的列 这是我想要完成的Diagram。 我有这段代码:

SELECT * FROM CustomerTable c
LEFT JOIN CustomerTableTemp ct ON c.IDCust = ct.IDCust
WHERE   c.IDCustomer =12

此代码显示所有列,但不显示具有不同值的特定列。

4 个答案:

答案 0 :(得分:0)

您可以使用内部联接

select customer.CustID 
, customer.Age
, temp.Age
from CustomerTable customer
inner join CustomerTableTemp temp
on customer.CustID = temp.CustID
and customer.Age <> temp.Age

答案 1 :(得分:0)

现在无法访问我的控制台尝试此操作。但是看看MYSQL NOT IN()函数 - 或尝试使用DISTINCT,以便MySQL删除重复项。

答案 2 :(得分:0)

试试这个

    Create table #Customer (CustId int, Fname VARCHAR(10),Lname VARCHAR(10),Age Int, Active bit)
    Create table #CustomerTemp (CustId int, Fname VARCHAR(10),Lname VARCHAR(10),Age Int, Active bit)

    insert into #Customer values(12,'first1','last',25,1)
    insert into #CustomerTemp values(12,'first2','last',18,1)

    insert into #Customer values(13,'first13','last1',20,1)
    insert into #CustomerTemp values(13,'first15','last2',20,1)

    SELECT * into #tmpTable From #Customer

    insert into #tmpTable
    Select * from #CustomerTemp


    Create Table #tmp2(CustId int, ColumnName VARCHAR(50),Value1 VARCHAR(10),Value2 VARCHAR(10))


    ;with cte
    AS
    (
        SELECT t.CustId, tpiv.* 
        FROM #tmpTable as t
        CROSS APPLY
        (
            VALUES      
                ('Fname',t.Fname),  
                ('Lname', t.Lname),
                ('Age', CONVERT(VARCHAR(10),t.Age))

        ) AS tPiv (ColumnName, value)   
    )

    INSERT INTO #tmp2
    SELECT t1.CustId,t1.ColumnName,t1.Value as Value1 ,t2.Value as Value2
    from cte t1
    JOIN cte t2 on t1.CustId=t2.CustId and t1.ColumnName=t2.ColumnName
    LEFT JOIN #tmp2 t3 on t3.CustId=t1.CustId and t3.ColumnName=t1.ColumnName
    WHERE t1.Value<>t2.Value 
    AND t3.CustId IS NULL 

    ;with cte1
    AS
    (
        SELECT ROW_NUMBER()OVER(Partition By CustId, ColumnName Order by CustId, ColumnName) as [Rank]
        ,* From #tmp2
    )

    DELETE FROM cte1 WHERE [Rank]>1

    select CustId,
      max(case when columnname = 'FName' then Value1 end) Fname,
      max(case when columnname = 'FName' then value2 end) Fname,
      max(case when columnname = 'LName' then value1 end) Lname,
      max(case when columnname = 'LName' then value2 end) Lname,
      max(case when columnname = 'Age' then value1 end) Age,
      max(case when columnname = 'Age' then value2 end) Age,
       max(case when columnname = 'Active' then value1 end) Active,
      max(case when columnname = 'Active' then value2 end) Active 

    from #tmp2
    Group by CustId


    Drop table #Customer
    Drop table #CustomerTemp
    Drop table #tmpTable
    DROP TABLE #tmp2

答案 3 :(得分:0)

您可以使用union和inner连接来查找值不同的所有记录。您可以根据需要添加任意数量的联合。

select customer.CustID 
, 'FName' 'field'
, customer.Fname 'current'
, temp.Fname 'temporary'
from CustomerTable customer
inner join CustomerTableTemp temp
on customer.CustID = temp.CustID
and customer.Fname <> temp.Fname    
union
select customer.CustID 
, 'LName' 'field'
, customer.LName 'current'
, temp.LName 'temporary'
from CustomerTable customer
inner join CustomerTableTemp temp
on customer.CustID = temp.CustID
and customer.LName <> temp.LName
union
select customer.CustID 
, 'Age' 'field'
, customer.Age 'current'
, temp.Age 'temporary'
from CustomerTable customer
inner join CustomerTableTemp temp
on customer.CustID = temp.CustID
and customer.Age <> temp.Age
union
select customer.CustID 
, 'Active' 'field'
, customer.Active 'current'
, temp.Active 'temporary'
from CustomerTable customer
inner join CustomerTableTemp temp
on customer.CustID = temp.CustID
and customer.Active <> temp.Active