I have 2 tables named projects
and tracklines
Every project can have multiple tracklines.
I have an SQL statement that will loop through my projects, but I want to be able to JOIN the trackline table with only 1 row. What I want to do is get all tracklines that are part of a project, order them based on more than one field and do a LIMIT 1 on it so that I have one trackline which I'm joining with my project.
This is what I have so far:
SELECT Project.* FROM `project` AS `Project`
LEFT JOIN (
SELECT *
FROM `trackline`
WHERE `trk_deleted` = '0'
ORDER BY `trk_state`, `trk_status`
LIMIT 1
) t ON t.`trk_project` = `prj_id`
ORDER BY `prj_name`
The problem is that I am not getting the trk_state
and trk_status
values in my outer query.
I was thinking that my subquery could have WHERE trk_project
= prd_id
but I am not sure how to get that to happen.