I'm trying to retrieve a set of data from two related tables (control_id links the two).
table1:
control_id (auto inc)
description VARCHAR(25)
...
table2:
control_id int
memo text
timestamp timestamp
I'm trying to write an sql join to gather the memo field from the most recent entry (ONLY). In the case of:
SELECT * FROM table1 t1 LEFT JOIN table2 t2 USING (control_id) ORDER BY timestamp DESC
This can result in multiple entries for the same control_id (different dates). I want one entry for each control_id based on table1 with the most recent data in the memo field from table2 for the control_id.
Using a loop and two selects:
SELECT * FROM table1;
for (each row)
SELECT memo FROM table2 WHERE control_id=row->control_id ORDER BY timestamp DESC LIMIT 1
I'm using mySql, any suggestion are appreciated.