1

我目前有一个表 project(id) 和一个表 project_step (id, project,step(integer))。

1 个项目链接到 1 个或多个 project_step。

我想获得一个项目列表,其中最后输入的 project_step (project_step.id DESC) 有一个 step = X。

我需要在子查询中执行此操作,但我不知道如何编写它。

4

2 回答 2

1

此查询将返回两个表中的所有列。额外子查询的目的是分别获取ID每个projecton table的最新信息project_step

SELECT  a.*, b.*
FROM    project a
        INNER JOIN project_step b
            ON a.ID = b.project
        INNER JOIN
        (
            SELECT  project, MAX(ID) ID
            FROM    project_step
            GROUP   BY project
        ) c ON b.project = c.project AND
                b.ID = c.ID
WHERE   b.step = ?
于 2013-07-11T14:48:36.877 回答
0

尝试这样的事情。

SELECT project.* from project_step
JOIN project ON project.id = project_step.project 
GROUP BY project
HAVING SUBSTRING_INDEX(GROUP_CONCAT(step ORDER BY step DESC SEPARATOR ' '), ' ', 1) = X
于 2013-07-11T14:52:47.760 回答