我正在尝试按照此答案中的说明如何获取连接表中的最新行。
我有两张桌子。
Projects : id, title
Status : project_id, status_id, created(DATETIME)
当我知道项目 ID(示例 = 2)时,我有正确的查询来选择最新的状态更新。
SELECT projects. * , project_state_project_map.status_id AS status,
project_state_project_map.created AS status_created
FROM projects
LEFT JOIN (
SELECT *
FROM project_state_project_map
WHERE project_id = 2
ORDER BY created DESC
LIMIT 1
)
project_state_project_map ON project_state_project_map.project_id = projects.id
WHERE projects.id = 2
LIMIT 1
但是,我无法弄清楚如何选择所有具有当前状态的项目。我必须对 sql 进行什么更改才能获得所有项目的最新状态。