I have two tables, with one containing records referencing the other:
Goal
id (int)
name (text)
value_mask (text)
GoalStatus
id (int)
goal (int)
created (datetime)
value (text)
Goal.id == GoalStatus.goal
What I'd like to do, is pull the latest record from the GoalStatus table, for each record in Goal. At the moment the only way I know how to do this is by making a separate query for each record in Goal (pseudocode):
goals = db.query("SELECT * FROM Goal")
foreach (goals as goal):
goalStatus = db.query("
SELECT * FROM GoalStatus
WHERE goal = " + goal.id + "
ORDER BY created DESC
LIMIT 1
")
Is there a way of condensing this, so I'm not making an extra query for each Goal?