这是一个 MySql 查询,用于打印构建的最后一个结果:
select * from buildresults where status in ('success','failed')
and projectId=4 order by id desc limit 1
如何为一组 projectId 运行 for_each 等效查询?对于每个项目,我需要确定最后一次构建是成功还是失败。我不想考虑具有运行状态的构建。
我有另一个查询返回一组 projectId。
您想要的是将每个结果限制为 projectId 的最后一个。
select * from buildresults
where status in ('success','failed')
and id =
(
select max(id) from buildresults as b2
where b2.status in ('success','failed') and
b2.projectId = buildresults.projectId
)
你可以做这样的事情
select * from buildresults
where status in ('success','failed')
and projectId IN
(SELECT projectId from other_table where other condition)
order by id desc
如果你想要最后的成功和失败,你可以试试这个
SELECT MAX(id),status FROM buildresults
WHERE status IN ('success','failed')
GROUP BY status
select * from buildresults where status in ('success','failed')
group by project_id order by id desc
然后,您可以循环遍历所有结果,每行 1 个。