简体   繁体   中英

Looping through a SQL QUERY record result from a Temp Table and Updating another table based on SQL QUERY results

I was having difficulty to formulate the question.But ,i better describe it Technically Here.

I want to Update let say User Table which has the following form:

--------------------------------------------------------------
UserID  ROLE_NAME VALUE
--------------------------------------------------------------
001       VALUE_Q   xx
002       VALUE_W   xx
003       VALUE_Q   xx
--------------------------------------------------------------

and Likes Id related to user many to many in seprate table like this

--------------------------------------------------------------
LikeId    Likes VALUE
--------------------------------------------------------------
001       Code   xx
002       Fun    xx
003       Eat    xx
--------------------------------------------------------------

and ,another table which has relation with User Table to Many to Many has the following information Let Say UserToLikeingTable


UserToLikingID  UserId LikeId
--------------------------------------------------------------
01              001     001
02              002     001
03              002     002
--------------------------------------------------------------

And ,here comes another table which holds UserId ,directly from UserTable

--------------------------------------------------------------
TESTTABLEID  USER_ID VALUE
--------------------------------------------------------------
001          001      100
002          002      220
003          003      400
004          002      231
--------------------------------------------------------------

And what i want is ,First List out the Duplicate User who share the same Liking and I have done that in a #TempTable as Follow

--------------------------------------------------------------
LikeID  UserId VALUE
--------------------------------------------------------------
001       001   100
001       002   200
002       003   400
002       004   500
003       005   211
003       005   210  
--------------------------------------------------------------

NOW comes my question ...

I want to update all user who share the same LikeID from the above #TempTable result into TESTTABLE as Follow: My End Result shoudl be like : Updating The UserId with LikeID for all LikeIDs in the previous result.


TESTTABLEID  USER_ID VALUE
--------------------------------------------------------------
001          001      100
002          001      220
003          002      400
004          002      500
--------------------------------------------------------------

Any help is much much i appreacted? How to run in loop to udpate the values?

Hint :

Update TestTable set UserId = #Temptable.LikeId
FROM #Temptable
where #Temptable.UserId = TestTable.UserId

And what if i have to update and delete duplicate UserId first and sum thier value and store one representative LikeId for them ?

instead of replacing all userId by LikeId you sum the value the have and replace one LikeId ?

Please, use a join as per reuirement and here sql look like :

 Update TestTable set UserId = #Temptable.LikeId
     from TestTable as test
    inner join  #Temptable on  #Temptable.UserId = test.UserId

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