我一直在比較兩個幾乎相同的表,並在第二個表中更新/添加結果。這裏是我使用的兩張桌子。 Person1(ID,姓名,PHNumber)和Person2(ID,姓名,PHNumber,IsActive)。比較兩個表,並在第二個表中添加結果
注意: person1總是比person2少/相同的行.Person1是參考表,我們需要添加/刪除person1 int中存在/不存在的行。 我正在使用Microsoft SQL Server管理工作室。
案例1:如果比較的結果有更多的行(讓我們說PERSON1有10家,PERSON2具有8rows),那麼我們需要添加這些(2rows)到PERSON2並保持IsActive-1的行。
案例2:如果比較結果有差異(比如說person1有10個,但是person2有20個),那麼我們需要找到這些delta 10rows併爲這10個行保留IsActive-0。
希望這個查詢很清晰,並期待您的寶貴解決方案。謝謝。
Here Person1 does not contain newdata2 and contains newdata,newdata1 which are
new values after comparing, so we are giving IsActive-0 and IsActive-1
respectively.
Person1:
ID Name PHNumber
1 \t missouri 123
2 \t kansas 111
3 \t stlouise 234
4 \t california 456
5 \t india 888
6 \t srilanka 780
7 \t dallas 890
8 \t texas 1111
9 \t mario 1112
10 \t sister 7878
11 \t pontiac 8765
12 \t newdata \t 1234
13 \t newdata1 2345
Person2:
ID Name PHNumber IsActive
1 \t missouri 123 1
2 \t kansas 111 1
3 \t stlouise 234 1
4 \t california 456 1
5 \t india 888 1
6 \t srilanka 780 1
7 \t dallas 890 1
8 \t texas 1111 1
9 \t mario 1112 1
10 \t sister 7878 1
11 \t pontiac 8765 1
12 newdata2 987 1
RESULT: Person2:
ID Name PHNumber IsActive
1 \t missouri 123 1
2 \t kansas 111 1
3 \t stlouise 234 1
4 \t california 456 1
5 \t india 888 1
6 \t srilanka 780 1
7 \t dallas 890 1
8 \t texas 1111 1
9 \t mario 1112 1
10 \t sister 7878 1
11 \t pontiac 8765 1
12 newdata2 987 0
13 newdata \t 1234 1
14 \t newdata1 2345 1
哪個匹配列(其中列中的值是相同的)使行「平等「到另一排? – granadaCoder
這很簡單。請嘗試使用合併/更新聲明並返回給我們。 https://technet.microsoft.com/en-us/library/bb522522(v=sql.105).aspx –
MySQL或SQL服務器?你們都有標籤......你的意思是? – pmbAustin