1

您好我在以下查询中遇到问题:

Update tbl 
set somecol = somecol 
Where Key = (select key  
             from tbl
             group by key 
             having count(*) > 1)  
  and Time = (select max(time) from tbl) 

当只有一个键时,上述查询工作正常。但是,如果有多个键,则此查询不起作用。如何存储来自选择子查询的多个值?时间列也可以是多个。我是 sql 新手。请指导。提前致谢。

4

2 回答 2

3

使用IN谓词:

Update tbl 
set somecol = somecol 
Where Key in (select key  
             from tbl
             group by key 
             having count(*) > 1)  
  and Time = (select max(time) from tbl)
于 2013-07-31T11:57:05.943 回答
1

尝试以下操作:

Update tbl 
set somecol = somecol 
Where Key in (select key  
             from tbl
             group by key 
             having count(*) > 1)  
  and Time in (select max(time) from tbl) 

根据要求,您还可以使用派生表。

于 2013-07-31T12:00:09.727 回答