Mysql – Adding a column to aggregated data where new value only applies to a subset of the aggregation

aggregatejoin;MySQL

Having trouble explaining this.

I have a table like this:

Label | Value
A     |   3
B     |   1
C     |   4

And another like this:

Label | Value | ID
A     | 1     | ID1
A     | 1     | ID2
C     | 4     | ID3

I need to add the ID column to the first table, splitting some rows according to the Value in the second table. Resulting in this:

Label | Value | ID
A     | 1     | ID1
A     | 1     | ID2
A     | 1     | NULL
B     | 1     | NULL
C     | 4     | ID3

If the Label and Value columns were identical for both tables, I could easily do join table2 ON table1.Label=table2.Label and table1.Value=table2.Value but the unaggregation aspect of this is throwing me off.

Apologies if this is worded poorly.

Best Answer

SELECT  `Label`, `Value`, ID
    FROM  table1
    LEFT JOIN  table2 USING(Label, Value);