首页 > 解决方案 > 如果主键在第二个表内,则更改行

问题描述

在此处输入图像描述

如果 order_no 在今天发运的表中,我想在查询中使用 sql 将订单状态更改为发运。(我使用 ms 访问)

标签: sqldatabasems-access

解决方案


您可以使用where子句。例如:

update orders
    set State = 'shipped'
    where order_no in (select ts.order_no from todayshipped as ts);

您可能需要添加and State <> 'shipped'以避免更新已发送的行。

出于性能原因,我经常建议exists

update orders
    set State = 'shipped'
    where exists (select 1
                  from todayshipped as ts
                  where ts.order_no = orders.order_no 
                 );

这可以很容易地利用上的索引todayshipped(order_no)


推荐阅读