1

我有 2 张桌子:

桌子workflow

Request_Id, Step ID
123, 4
123, 7
198, 5

桌子Steps

Step_Id, Step_num
4, 30
7, 12
5, 172

我正在尝试返回表 Workflow 中的所有记录,其中 Steps.step_num 是该记录的最大值。我的结果应该是这样的:

Request_Id, Step_Id, Step_Num
123, 4, 30
198, 5, 172

我试过这个,但我没有得到正确的 Step_ID:

select request_id, step_id,max(step_num) from
(
select ww.request_id as request_id, ww.step_id as step_id, st.step_num as step_num
from workflow ww
left join steps st on ww.step_id=st.step_id) as s
group by request_id

请帮忙,我卡住了:)

4

1 回答 1

1
SELECT  *
FROM    (
        SELECT  DISTINCT request_id
        FROM    workflow
        ) wd
JOIN    steps s
ON      s.step_id =
        (
        SELECT  si.step_id
        FROM    workflow wi
        JOIN    steps si
        ON      si.step_id = wi.step_id
        WHERE   wi.request_id = wd.request_id
        ORDER BY
                stem_num DESC
        LIMIT 1
        )
于 2012-06-06T08:46:40.583 回答