我有一张像这样的表'虚拟'
name, status , date, is-updated
n1, s1, 2013-09-01, false
n2, s2, 2013-09-01, false
n1, s11, 2013-09-02, false
现在为了获取特定名称的最新时间戳,我正在使用查询:
select name, status, max(date)
from dummy
where is-updated=false
group by name,status
这正常工作正常,返回两行
n2, s2, 2013-09-01, false
n1, s11, 2013-09-02, false
现在如何将上面两行的'is-updated'值更新为'true'? sql中的'IN'子句不支持多列。
update dummy
set is-updated = true
where name, status, date in (
select name, status, max(date)
from dummy
where is-updated=false
group by name,status
)
任何人都可以帮忙吗?感谢。
答案 0 :(得分:0)
给我一些更多信息,我会更新这个更好:
update dummy as d
join (
select name, max(modifieddate) as max_mod
from dummy
where is_updated = FALSE
group by name ) as max_mod_tbl
on max_mod_tbl.name = d.name and
max_mod_tbl.max_mod = d.modifieddate and
set is_updated = true
where d.is_updated = false
http://www.sqlfiddle.com/#!2/26bad/1/0
我添加了rows_you_want_to_change.is-updated = d.is-updated
,因为...如果您有两行elect name, status, max(date) as dt
和is-updated
种状态
喜欢关键字段。并且该查询与您说它返回的行不匹配..?
答案 1 :(得分:0)
您可以使用update
和join
执行此操作。正确的语法是:
update dummy join
(select name, status, max(date) as maxdate
from dummy
where `is-updated` = false
group by name, status
) toupdate
on dummy.name = toupdate.name and
dummy.status = toupdate.status and
dummy.date = toupdate.maxdate
set `is-updated` = true;
join
执行过滤,因此您不需要where
子句。