问题
Is there a way to compare 3 sql tables (A, B, C)? Which A and B have columns labled Name, TimeStamp in A, and Name, ScheduledTime in B. I need to compare these 2 columns and if they do not match up, put a numeric value of 1 into column 2 next to the name in table C. I'm fairly new to SQL but wasn't sure if this was possible. (Using SSMS)
Table A looks like this.
Name | Time
-----------------
John | 7:00AM
Joe | 7:07AM
Tim | 7:25AM
Table B looks like this.
Name | Schedule
--------------------
John | 7:00AM
Joe | 7:00AM
Tim | 7:00AM
Table C should look like this.
Name | Number
-----------------
John | 0
Joe | 1
Tim | 1
回答1:
For your sample data, the following will work:
select a.name, (case when a.time = b.schedule then 0 else 1 end) as number
into c
from a join
b
on a.name = b.name;
It is not clear if that is a general solution, but it works for the provided data.
EDIT:
SQL Fiddle does not seem to be running MS SQL code right now. But here is a SQL Fiddle that I'm confident will show that the above works.
来源:https://stackoverflow.com/questions/34536058/compare-2-sql-tables-if-data-does-not-match-put-an-integer-value-in-3rd-table