2011-11-28 54 views
0

我正尝试使用更新语句中由count重新调用的值。但得到语法错误。子查询正在工作。但更新查询不起作用。需要做出什么改变才能获得正确的结果?如何在sql中使用计数的返回值

update Items set numberOfBids = count (select * from Bids where id ='1673078805') where id='1673078805';  
Error: near "select": syntax error 


sqlite> select * from Bids where id ='1673078805'; 
1673078805|brettgodfrey|14.52|2001-12-05 10:40:41 
1673078805|gardnerstoys|15.04|2001-12-06 07:40:41 
1673078805|tallulahbankhead|14.01|2001-12-04 13:40:41 
1673078805|[email protected]|15.55|2001-12-07 04:40:41 
1673078805|yesterdaysgem|16.06|2001-12-08 01:40:41 


sqlite> update Items set numberOfBids = 5 where id='1673078805'; 

回答

5

移动计数到子查询:

update Items 
set numberOfBids = 
    (select count(*) 
     from Bids 
     where id ='1673078805') 
where id='1673078805'; 
1

试试这个SQL:

UPDATE Items 
SET numberOfBids = (SELECT count(*) 
        FROM Bids 
        WHERE id ='1673078805') 
WHERE id='1673078805';