Summary of how to compare the data of each group of two tables in SQLServer-mysql tutorial

Source: Internet
Author: User
This article briefly introduces several methods for comparing data of two tables in SQLServer. For more information, see.

This article briefly introduces several methods for comparing data of two tables in SQL Server. For more information, see.

The code is as follows:


Use tempdb
Go
If object_id ('Table _ left') is not null drop table table_left
If object_id ('Table _ right') is not null drop table table_right
Go
Create table table_left (groupId nvarchar (5), dataSub1 nvarchar (10), dataSub2 nvarchar (10 ))
Create table table_right (groupId nvarchar (5), dataSub1 nvarchar (10), dataSub2 nvarchar (10 ))
Go


Alter table table_left add dataChecksum as checksum (dataSub1, dataSub2)
Alter table table_right add dataChecksum as checksum (dataSub1, dataSub2)
Go

Create nonclustered index ix_table_left_cs on table_left (dataChecksum)
Create nonclustered index table_right_cs on table_right (dataChecksum)


Go
Set nocount on
Go
Insert into table_right (groupId, dataSub1, dataSub2)
'# 1', 'data1', 'data7' union all
Select '# 1', 'data2', 'data8' union all
Select '# 1', 'data3', 'data9' union all
Select '# 2', 'data55', 'data4' union all
Select '# 2', 'data55', 'data5'


Insert into table_left (groupId, dataSub1, dataSub2)
Select '# 11', 'data1', 'data7' union all
Select '# 11', 'data2', 'data8' union all
Select '# 11', 'data3', 'data9' union all
Select '# 22', 'data55', 'data0' union all
Select '# 22', 'data57', 'data2' union all
Select '# 33', 'data99', 'data4' union all
Select '# 33', 'data99', 'data6'


Go
-- Select

Select distinct a. groupId
From table_left
Inner join table_right B on B. dataChecksum = a. dataChecksum
And B. dataSub1 = a. dataSub1
And B. dataSub2 = a. dataSub2
Where not exists (select x. dataSub1, x. dataSub2 from table_left x where x. groupId =. groupId limit T select y. dataSub1, y. dataSub2 from table_right y where y. groupId = B. groupId)
And not exists (select x. dataSub1, x. dataSub2 from table_right x where x. groupId = B. groupId limit T select y. dataSub1, y. dataSub2 from table_left y where y. groupId =. groupId)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.