0

我有一张像这样的“假人”表

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

)

有人能帮忙吗 ?谢谢。

4

2 回答 2

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状态

像关键领域。并且该查询与您说它返回的行不匹配..?

于 2013-09-06T18:40:54.573 回答
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子句。

于 2013-09-06T18:45:07.210 回答