3

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. I've made use of the distinct command because a customer can have multiple loans.

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. This leads to all customers' attribute passworddisabled being set to 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

customer_id name passworddisabled
1 Pedro FALSE
2 Sandra FALSE
3 Peter TRUE
4 Norman TRUE
loan_id loan_customerid loan_cashregister
1 1 1
2 1 1
3 4 2
4 2 1

In this case, Pedro and Sandra's passworddisabled attribute should be set to true because they have loans with cash register 1.

Let me know if you need more info.

Thanks again!

forpas
  • 160,666
  • 10
  • 38
  • 76
spoons
  • 75
  • 5
  • Some workable data please – Asgar Jul 13 '22 at 12:51
  • 1
    you should always tag the database product you use, and also provide sample data and expected output. Thanks. – ADyson Jul 13 '22 at 12:53
  • Does this answer your question? [How can I do an UPDATE statement with JOIN in SQL Server?](https://stackoverflow.com/questions/1293330/how-can-i-do-an-update-statement-with-join-in-sql-server) – Natrium Jul 13 '22 at 13:15
  • As per [ask] please don't provide images of code or data. We can't re-use a picture in an answer or demo, or use it to test our solutions! If something is naturally text, then provide it in your question as text. The question editor has tools for formatting tables. – ADyson Jul 13 '22 at 13:31
  • 1
    @ADyson thanks for the feedback! I've ammended my question accordingly. – spoons Jul 13 '22 at 14:29
  • 1
    @forpas, done. I think the sample data makes sense now in the context of the use case. – spoons Jul 13 '22 at 14:50

2 Answers2

4

This is the correct syntax for Postgresql's join-like UPDATE statement:

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:

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.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • Hi @forpas! Thanks for the solution and introducing me to that awesome dbfiddle tool! Much appreciated! – spoons Jul 13 '22 at 15:15
1

If it is MySQL, Try this:

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

EDIT Matter of fact, you may not even need the subquery, joining loan, should work just fine:

UPDATE 
customer c
JOIN loan l ON c.customer_id = l.loan_customerid
SET passworddisabled = TRUE 
WHERE l.loan_cashregisterid = '1'
Asgar
  • 1,920
  • 2
  • 8
  • 17
  • Hi Asgar, I tried out both queries but they're not working. Is this a syntax used solely by MySQL and not postgres? Literally every tutorial I've looked at suggests putting set as the second line. Both queries return the following: ERROR: syntax error at or near "JOIN" – spoons Jul 13 '22 at 14:21
  • You didnt mention your database earlier, so I wrote query for MySQL. Replace JOIN with INNER JOIN – Asgar Jul 13 '22 at 14:23
  • No worries. Sorry, that was my fault. – spoons Jul 13 '22 at 15:15