简体   繁体   中英

Sql left join on Id and date

I have a table A that contain a record Id and a date column.

I need left join this with another table B by the same record Id and by the date so that I only show the records from the first table B for the given record Id for that date.

Table A:

2017-11-03 | 123456 | 5 | 6 

Table B:

2017-11-03 | 123456
2017-11-05 | 123456

When join together by Id and date I want to see:

2017-11-03 | 123456 | 5 | 6
2017-11-05 | 123456

A left join, by definition, only shows rows from the left table-in this case, B. Any rows in B that are not matched, show nulls in the table A columns. So, ths code should produce the output you want.

SELECT *
FROM B
LEFT JOIN B ON A.ID = B.ID AND A.Date = B.Date

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