简体   繁体   English

使用 join 和 where 子句更新查询

[英]Update query with join and where clause

I'm currently confused with a (fairly easy) update statement that I'm trying to execute on a table.我目前对我试图在表上执行的(相当简单的)更新语句感到困惑。 The two tables are as such:这两个表是这样的:

  1. Customer table has the columns客户表有列

    customer_id [string] passwordisabled [boolean]
  2. Loan table has the columns贷款表有列

    loan_id [string], customer_id [string & foreign key], cashregister_id [string]

I would like to update the passworddisabled attribute to true if they are registered via a specific cash register.如果它们是通过特定的收银机注册的,我想将passworddisabled属性更新为true I've made use of the distinct command because a customer can have multiple loans.我使用了distinct命令,因为客户可以拥有多笔贷款。

Here is what I've tried:这是我尝试过的:

update customer
set passworddisabled = true
from customer c
    join (select distinct loan_customerid, loan_cashregisterid
          from loan
         ) l
     on c.customer_id = l.loan_customerid  
where l.loan_cashregisterid = '1' 

What seems to be happening is that my where clause is being ignored entirely.似乎正在发生的事情是我的 where 子句被完全忽略了。 This leads to all customers' attribute passworddisabled being set to true .这导致所有客户的属性passworddisabled设置为true I'm not entirely sure what this is happening so I would be really appreciative of some advice regarding what this query is actually doing and how to fix it.我不完全确定这是怎么回事,所以我非常感谢一些关于这个查询实际在做什么以及如何解决它的建议。

Here is some workable data: Customer 1---* Loan以下是一些可行的数据: 客户 1---* 贷款

customer_id客户ID name姓名 passworddisabled密码禁用
1 1 Pedro佩德罗 FALSE错误的
2 2 Sandra桑德拉 FALSE错误的
3 3 Peter彼得 TRUE真的
4 4 Norman诺曼 TRUE真的
loan_id贷款编号 loan_customerid贷款客户ID loan_cashregister贷款收银机
1 1 1 1 1 1
2 2 1 1 1 1
3 3 4 4 2 2
4 4 2 2 1 1

In this case, Pedro and Sandra's passworddisabled attribute should be set to true because they have loans with cash register 1.在这种情况下,Pedro 和 Sandra 的 passworddisabled 属性应该设置为 true,因为他们有收银机 1 的贷款。

Let me know if you need more info.如果您需要更多信息,请告诉我。

Thanks again!再次感谢!

This is the correct syntax for Postgresql's join-like UPDATE statement:这是 Postgresql 的 join-like UPDATE语句的正确语法:

UPDATE customer AS c
SET passworddisabled = true
FROM loan AS l
WHERE c.customer_id = l.loan_customerid AND l.loan_cashregister = '1'; 

See the demo .请参阅演示

But I would suggest the use of EXISTS :但我建议使用EXISTS

UPDATE customer AS c
SET passworddisabled = EXISTS (
                         SELECT * 
                         FROM loan AS l 
                         WHERE c.customer_id = l.loan_customerid AND l.loan_cashregister = '1'
                       ); 

See the demo .请参阅演示

If it is MySQL, Try this:如果是 MySQL,试试这个:

UPDATE 
customer c
JOIN (SELECT DISTINCT loan_customerid, loan_cashregisterid FROM loan ) l ON c.customer_id = l.loan_customerid
SET passworddisabled = TRUE 
WHERE l.loan_cashregisterid = '1'

For update queries, Always mention your table names and relations before SET对于更新查询,请始终在SET之前提及您的表名和关系

EDIT Matter of fact, you may not even need the subquery, joining loan , should work just fine:编辑事实上,您甚至可能不需要子查询,加入loan ,应该可以正常工作:

UPDATE 
customer c
JOIN loan l ON c.customer_id = l.loan_customerid
SET passworddisabled = TRUE 
WHERE l.loan_cashregisterid = '1'

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

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