sql - 如果主键在第二个表内,则更改行
问题描述
如果 order_no 在今天发运的表中,我想在查询中使用 sql 将订单状态更改为发运。(我使用 ms 访问)
解决方案
您可以使用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)
。
推荐阅读
- python-3.x - 将列与 pandas_schema 进行比较
- swift - 我的 Cocoapods 框架方法不能通过在另一个应用程序中导入来使用
- spring-boot - 使用 Maven 将环境变量传递给 Spring Boot 应用程序
- powershell - 在 Powershell 中将字符串从 CSV 转换为 DateTime
- r - 计算字符串单元格中的多个数据
- phpstorm - 如何在 PhpStorm 中通过文件的相对路径打开文件?
- postgresql - Makefile:运行命令“go test ./...”后终止
- javascript - Chart.js - 使用工具提示绘制任意点
- elasticsearch - Logstash 无效的字段引用:`[iso-8859-1]to`
- javascript - 多系列 d3js v5 图表的折线图缩放