在UPDATE查询中使用SELECT

时间:2010-12-18 17:11:26

标签: sql ms-access select

我尝试过这个查询,但它不起作用......任何人都可以帮助我

(SELECT (r.fare-r.fare*(t.discount/100)) FROM ccard AS c, downer AS d, 
    typepolicy AS t, 
rates AS r
WHERE c.cardno=Enter_card_no and d.deviceno=Enter_device_no and 
    d.routeno=r.routeno and c.type=t.type );

UPDATE bank 
SET amount = amount-[the_result of above select query]
WHERE accountno=(select c.accountno from ccard c where cardno=Enter_card_no);

所以我尝试了

UPDATE bank 
SET amount = amount-(SELECT (r.fare-r.fare*(t.discount/100)) FROM ccard AS c, 
    downer AS d, typepolicy AS t, rates AS r
WHERE c.cardno=Enter_card_no and d.deviceno=Enter_device_no and 
    d.routeno=r.routeno and c.type=t.type )
WHERE accountno=(select c.accountno from ccard c where cardno=Enter_card_no);

但是它会出错

  

“操作必须使用可更新的查询”

1 个答案:

答案 0 :(得分:0)

首先,关于查询可更新性的主题,如果使用WHERE子句连接表,则查询将不可更新。例如,而不是

FROM downer AS d, rates AS r
WHERE d.routeno=r.routeno

你应该使用

FROM downer AS d INNER JOIN rates AS r ON d.routeno=r.routeno

第二,[银行]是一排表吗?或者您是否真的想将金额列设置为所有行的相同值?

第三,在不知道如何连接示例中的所有表的情况下,不可能编写可更新的查询来执行您想要的操作。