SQL 从一张表更新另一张表

发布于 2024-08-08 18:59:21 字数 272 浏览 8 评论 0原文

这是我的问题的简化版本:

Table 1
Key1, lastdate, lasttranstype

Table2
Table1key1, trandate, trantype

”来更新该表中每条记录的“lastdate”和“lasttransdate”

我想要一个 SQL 语句使用表 2 中的匹配记录和最新日期“我的数据库正在处理

。这可能吗(我当然可以编写一个程序来做到这一点,但我宁愿只用 SQL 语句来做到这一点)?

Here’s a simplified version of my problem:

Table 1
Key1, lastdate, lasttranstype

Table2
Table1key1, trandate, trantype

I want an SQL statement to update lastdate and lasttransdate on table1 for each record in that table using the matching record in Table2 with the latest date

My DB is progress.

Is this possible (I can of course write a program to do it but I'd rather do it just with an SQL statement)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

☆獨立☆ 2024-08-15 18:59:21

在 T-SQL 中:

UPDATE Table1 
SET lastdate = trandate, lasttranstype = trantype
FROM Table1 INNER JOIN Table2 ON Table1.Key1 = Table2.Table1Key1

编辑:
我不知道 Progress 是 RDBMS 的名称。以下内容对您有用吗?

UPDATE Table1
SET lastdate = 
(SELECT trandate FROM Table2 WHERE Table2.Table1Key1 = Table1.Key1),
lasttranstype = 
(SELECT trantype FROM Table2 WHERE Table2.Table1Key1 = Table1.Key1)

In T-SQL:

UPDATE Table1 
SET lastdate = trandate, lasttranstype = trantype
FROM Table1 INNER JOIN Table2 ON Table1.Key1 = Table2.Table1Key1

EDIT:
I did not know that Progress was the name of a RDBMS. Would the following work for you?

UPDATE Table1
SET lastdate = 
(SELECT trandate FROM Table2 WHERE Table2.Table1Key1 = Table1.Key1),
lasttranstype = 
(SELECT trantype FROM Table2 WHERE Table2.Table1Key1 = Table1.Key1)
疯到世界奔溃 2024-08-15 18:59:21
update table1, table2 
set table1.lastdate = table2.trandate, table1.lasttranstype = table2.trantype 
where table1.key1 = table2.table1key1
update table1, table2 
set table1.lastdate = table2.trandate, table1.lasttranstype = table2.trantype 
where table1.key1 = table2.table1key1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文