简体   繁体   English

更新行检查重复项

[英]update row check duplicates

I hope you can help me with the correct SQL syntax. 希望您能为我提供正确的SQL语法。

main_accounts (table1)
'---id, group_name, account_name, payment_method

payments (table2)
'---id, account_name, payment_method

What I am trying to do is to update the account_name row in payments where payment_method is equal to payment_method in main_accounts table. 我想要做的是更新付款中的account_name行,其中payment_method等于main_accounts表中的payment_method

I tried: 我试过了:

update payments 
set account_name = (select account_name 
                    from main_accounts 
                    where payment_method = payment_method) 
WHERE payment_method = payment_method

but it's saying: 但是这是在说:

#1242 - Subquery returns more than 1 row #1242-子查询返回的行数超过1

So I don't know... I wish I could do this: 所以我不知道...我希望我可以这样做:

update payments 
set account_name = (select account_name 
                    from main_accounts 
                    where payment_method = **{is equal to payment_method in main_accounts table}**

Additionally, I wish I could make this as a trigger that when main_accounts table is updated, account_name goes automatically to payments table where the account_name is printed to the column when payment_method matches the payment_method in main_accounts . 此外,我希望我能做出这样的触发器,当main_accounts表被更新, account_name自动转到payments table ,其中account_name打印到列时payment_method比赛payment_methodmain_accounts

You are getting that error, cause your subquery is returning more than 1 value which can't be used in SET statement. 您收到该错误,因为您的子查询返回的值不止1个,而该值不能在SET语句中使用。 You should do a JOIN with other table in question while performing the UPDATE statement. 在执行UPDATE语句时,应该与其他有问题的表进行JOIN Something like 就像是

UPDATE payments a 
    JOIN main_accounts b ON a.payment_method = b.payment_method        
SET a.account_name = b.account_name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM