如何从select子查询中存储多个值

时间:2013-07-31 11:55:32

标签: sql

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

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

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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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) 

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