简体   繁体   中英

Update Table having two different column with same value in different rows

Here, my requirement is pretty amazing, need to update same table's column ParentLinkID = LinkID where Title equals to ParentLinkTitle. Note: Title and ParentLinkTitle are in different rows. How is it possible to Update ParentLinkID as mentioned here ?

CREATE TABLE #temp
(
LinkID INT,
Title NVARCHAR(100),
ParentLinkID INT,
ParentLinkTitle NVARCHAR(100)
)

INSERT INTO #temp
        ( LinkID ,
          Title ,
          ParentLinkID ,
          ParentLinkTitle
        )
VALUES  ( 1 , -- LinkID - int
          N'Dashboard' , -- Title - nvarchar(100)
          0 , -- ParentLinkID - int
          N'Ecommerce'  -- ParentLinkTitle - nvarchar(100)
        ),
        ( 2 , -- LinkID - int
          N'User' , -- Title - nvarchar(100)
          0 , -- ParentLinkID - int
          N''  -- ParentLinkTitle - nvarchar(100)
        ),
        ( 3 , -- LinkID - int
          N'Ecommerce' , -- Title - nvarchar(100)
          0 , -- ParentLinkID - int
          N'User'  -- ParentLinkTitle - nvarchar(100)
        ),
        ( 4 , -- LinkID - int
          N'Shipping' , -- Title - nvarchar(100)
          0 , -- ParentLinkID - int
          N'Business'  -- ParentLinkTitle - nvarchar(100)
        ),
        ( 5 , -- LinkID - int
          N'Product' , -- Title - nvarchar(100)
          0 , -- ParentLinkID - int
          N'Dashboard'  -- ParentLinkTitle - nvarchar(100)
        ),
        ( 6 , -- LinkID - int
          N'Business' , -- Title - nvarchar(100)
          0 , -- ParentLinkID - int
          N'Product'  -- ParentLinkTitle - nvarchar(100)
        );

SELECT * FROM #temp

--Base Table
LinkID  Title       ParentLinkID    ParentLinkTitle
1       Dashboard   0               Ecommerce
2       User        0   
3       Ecommerce   0               User
4       Shipping    0               Business
5       Product     0               Dashboard
6       Business    0               Product

Need to Update above #temp table's column ParentLinkID as below output:

--OUTPUT
LinkID  LinkTitle          IsActive ParentLinkID    ParentLinkTitle
1       Dashboard           1       3               Ecommerce
2       User        1       0       0
3       Ecommerce           1       2               User
4       Shipping            1       6               Business
5       Product             1       1               Dashboard
6       Business            1       5               Product

You can try self join with Left JOIN and coalesce function to check t2.LINKID is or isn't null

UPDATE t1
SET t1.ParentLinkID = coalesce(t2.LINKID,0)
FROM temp t1 LEFT JOIN temp t2
ON t1.ParentLinkTitle = t2.Title

sqlfiddle

UPDATE tmp

SET ParentLinkID = tmp1.LinkID
FROM temp  tmp
INNER JOIN temp as tmp1 ON tmp1.Title = tmp.ParentLinkTitle;

Check http://sqlfiddle.com/#!18/58be7/5

You can do it without any join too like below:

update temp 
set ParentLinkID = isnull((select LinkID from temp t2 where t2.Title = temp.ParentLinkTitle),0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM