Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

sql - How can I join two tables but only return rows that don't match?

I have two tables which look like this:

T1:  ID  |  Date  |  Hour  | Interval
T2:  ID  |  Date  |  Hour

I basically need to join these tables when their IDs, dates, and hours match. However, I only want to return the results from table 1 that do not match up with the results in table 2.

I know this seems simple, but where I'm stuck is the fact that there are multiple rows in table 1 that match up with table 2 (there are multiple intervals for any given hour). I need to return all of these intervals so long as they do not fall within the same hour period in table 2.

Example data:

T1:  1  |  1/1/2011  |  1  |  1
     1  |  1/1/2011  |  1  |  2
     1  |  1/1/2011  |  2  |  1
     1  |  1/1/2011  |  2  |  2

T2:  1  |  1/1/2011  |  1

My expected result set for this would be the last two rows from T1. Can anyone point me on the right track?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
SELECT T1.*
    FROM T1
    WHERE NOT EXISTS(SELECT NULL
                         FROM T2
                         WHERE T1.ID = T2.ID 
                             AND T1.Date = T2.Date
                             AND T1.Hour = T2.Hour)

It could also be done with a LEFT JOIN:

SELECT T1.*
    FROM T1
        LEFT JOIN T2
            ON T1.ID = T2.ID
                AND T1.Date = T2.Date
                AND T1.Hour = T2.Hour
    WHERE T2.ID IS NULL

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...