The question I have is kind of simular to this one – MySQL select 1 row from inner join
However, it was never really answered in the way that I needed it. So I thought I would ask.
I have a table A (tableA) and this table has a load of states in tableB. New states keep being added all the time.
TableB has a column ‘State’ which has a foreign key to TableA.Id. This table B has a value and timestamp
I am trying to get ONE query which will bring back ALL values in TableA, with an inner join to bring the LATEST ‘value’ of those rows from tableB. The latest being the one with the latest time.
I know how to do an inner join where it needs to be, and I know how to order a table to bring back the latest ‘time’ row, however I dont know how to put these two together and create a single query.
I’m sure I will have to have a Select within a select, which is fine. However, what I am trying to avoid is to bring a DataTable back with all my results from TableA and do a query for each of those on tableB seperately.
Any ideas?
EDIT – I have made a mistake with the question, I only really noticed when trying to implement one of the answers.
I have a foreign key between TableA.id and TableB.proId
TableA – ‘Id’, ‘Name’
TableB – ‘Id’, ‘proId’, ‘State’, ‘time’
I want to bring back all values of TableA with a join on B, to bring back the ‘State’ of the Max time
1 Answer