I have two tables I need to select data from TABLE_A
and TABLE_B
; they have a one to many relationship.
In my select statement I will often get multiple unique results from TABLE_A
and this is fine. But I will also get multiple matches in TABLE_B
- I need to get the most recent TABLE_B
record that matches. I have an auto incremented id tag that is available.
Here is a more detailed example:
TABLE_A
TABLE_A_id
, data
1, something
2, somethignelse
3, yetagainsomething
TABLE_B
TABLE_B_id
, TABLE_A_id
, data
1, 1, filler_data1
2, 1, filler_data1
3, 1, filler_data3
4, 2, filler_data4
5, 2, filler_data5
6, 3, filler_data1
I need to select the data such that my returned array is something like this for a search on rows containing "filler_data1":
TABLE_A_id
= 1, something, TABLE_B_id
= 2, filler_data1
TABLE_A_id
= 3, yetagainsomething, TABLE_B_id
= 6, filler_data1
So in the above case I get the TABLE_B
data which is the most recent, i.e. TABLE_B_id
= 2 and matches the search of "filler_data1".