我的表格如下所示:
id
caseid
status表格中的示例数据:
1 10 open
2 10 published
3 10 reopened
4 11 open
5 11 contact
6 11 response
7 11 published我需要获取上一次状态为published的所有caseid。
因此,在上面的示例中,将只检索11 (因为caseid 10后来被重新打开)。
查询应该是什么样子的?
PS
我将PDO与预准备语句一起使用。
发布于 2012-01-03 03:34:20
select caseid from data d1 where d1.status = 'published'
and not exists (select * from data d2 where d1.caseid = d2. caseid
and d2.id > d1.id)为了获得最佳性能,应该对id和caseid列进行索引。
发布于 2012-01-03 03:25:57
select a.caseid
from
(select max(id) as id, caseid
from table
group by caseid)a --this would get the last entry for every caseid
inner join table b on (a.id=b.id)
where b.status = 'published' 发布于 2012-01-03 03:26:36
编辑:
对不起--第一次脑筋发热:(
这样好点了吗?
select a.caseid,a.status
from temp a
where a.id in
(select MAX(id) maxid from temp group by caseid)
and status='published'https://stackoverflow.com/questions/8704511
复制相似问题