如何在sql中使用count的返回值

时间:2011-11-28 10:25:09

标签: sql count sql-update

我正在尝试使用update语句中count重新调整的值。但是获得语法错误。子查询正在运行。但更新查询无效。获得正确结果需要进行哪些更改?

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|tgrhino@home.com|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';

2 个答案:

答案 0 :(得分:5)

计数移动到子查询中:

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

答案 1 :(得分:1)

试试这个SQL:

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