我如何去匹配两个表中的三个字段,以便在匹配时更新相同的表

发布于 2024-08-11 19:39:31 字数 998 浏览 4 评论 0原文

给定MySQL数据库中的两个表A和B,当且仅当表A中上述行中的某些字段与表B中不同行中的字段完全匹配时,如何更新表A中的一行中的字段。每次比较时,表 B 中的行必须且仅使用一次。因此,表 B 中具有与表 A 中的行匹配的字段的行不能用于增益来匹配表 A 中的任何其他行。

不幸的是

UPDATE
    Table A,
    Table B
SET 
    Table A.Status = 'MATCHED',
        Table B.Status = 'USED'
WHERE
    Table B.Status IS NULL
AND 
    Table A.Field1 = Table B.Field1
AND 
    Table A.Field2 = Table B.Field2

,这不会给出我想要的结果,因为表 A 中的行不同倾向于与表 B 中的同一行进行匹配。

例如:表 A

ID  Date       Ref    Amount  Status 
1   2009-10-20 773    300000         
2   2009-10-20 773    10000   MATCHED
3   2009-10-20 773    150000         
4   2009-10-20 773    20000   MATCHED
5   2009-10-20 773    140000  MATCHED

表 B

 Ref Amount Date       ID
 870 50000  2009-11-01 1 
 871 50000  2009-11-01 2 
 871 80000  2009-11-01 3 
 871 20000  2009-11-01 4 
 871 20000  2009-11-01 5 
 871 20000  2009-11-01 6 
 872 300000 2009-11-01 7 

使用 Ref、Amount 和 Date 进行匹配。

Given two tables A and B in a MySQL Database, how can one update a field in a row of table A if and only if certain fields in the fore-mentioned row in table A exactly match fields in a distinct row of table B. The rows in table B must be used once and only once for each comparison. As such, a row in Table B that has fields matching a row in Table A can not be used a gain to match any other row in Table A.

I have tried

UPDATE
    Table A,
    Table B
SET 
    Table A.Status = 'MATCHED',
        Table B.Status = 'USED'
WHERE
    Table B.Status IS NULL
AND 
    Table A.Field1 = Table B.Field1
AND 
    Table A.Field2 = Table B.Field2

Unfortunately, this does not give my desired results since different rows from Table A tend to get matched with the same row in Table B.

For example: Table A

ID  Date       Ref    Amount  Status 
1   2009-10-20 773    300000         
2   2009-10-20 773    10000   MATCHED
3   2009-10-20 773    150000         
4   2009-10-20 773    20000   MATCHED
5   2009-10-20 773    140000  MATCHED

Table B

 Ref Amount Date       ID
 870 50000  2009-11-01 1 
 871 50000  2009-11-01 2 
 871 80000  2009-11-01 3 
 871 20000  2009-11-01 4 
 871 20000  2009-11-01 5 
 871 20000  2009-11-01 6 
 872 300000 2009-11-01 7 

To match using Ref, Amount and Date.

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

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

发布评论

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

评论(2

白芷 2024-08-18 19:39:31

是否可以像在 WHERE 子句中添加排除项以避免重用一样简单?

AND Table A.Status <> 'MATCHED'
AND Table B.Status <> 'USED'

如果每行依次更新,这将起作用,但如果它是基于设置和事务性的,则不起作用。我很抱歉没有对此进行测试;我这里没有 MySQL 来尝试这个。

Could it be as simple as adding exclusions to the WHERE clause to avoid reuse?

AND Table A.Status <> 'MATCHED'
AND Table B.Status <> 'USED'

This will work if each row is updated in turn, but not if it's set based and transactional. My apologies for not testing this; I don't have MySQL here to try this with.

有深☉意 2024-08-18 19:39:31

在 WHERE 子句中添加更多条件以强制 tableA 和 tableB 中的记录之间进行 1:1 匹配?

Add more conditions to your WHERE clause to force a 1:1 match between records in tableA and tableB?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文