考虑这两张桌子。
第一表
stageTable
舞台
1-开始
2-规划
3.工作
4
5?关闭。
第二表
stageProject
stageid??
1
2?
3
4
1-2
2
3
4
5?
1
2
3
我想要做的是从每个projectId中得到最大stageId值
所以我想以:
1-4
2-5
3-3
并从stage表中分配stageName,这样最终结果将是
项目-最大
1 4审查
2 5闭幕式
3.工作
我试过了
select a.projectid, max(a.stageid), b.stageName
from stageProject a, stageTable b
where a.stageId=b.stageId
group by a.projectId但不起作用
我以正确的stageId和max结尾,但是stageName总是相同的。
你能帮帮我吗!
发布于 2014-01-31 18:22:00
尝尝这个
select distinct projectid,
(select max(stageid)
from stageProject p2
where p1.projectId = p2.projectId),
(selec stageName from stageTable
where stageId = (select max(stageid)
from stageProject p2
where p1.projectId = p2.projectId))
from stageProject p1或者这个
select ps.projectid, ps.stageid, st.stagename
from stageTable st,
(select a.projectid, max(a.stageid) stageid
from stageProject a, stageTable b
where a.stageId=b.stageId
group by a.projectId) ps
where st.stageid = ps.stageidhttps://stackoverflow.com/questions/21487104
复制相似问题