Robert-Jan Kempink
Robert-Jan Kempink

Reputation: 11

Match value in copy copy over value if there is a match

I have 2 tables in my database

Customers table:

Customer_id Firstname Lastname
123156 John Johnsen

Orders table:

OrderID Firstname Lastname Customer_ID
9999999 John Johnsen NULL

I want to match the firstname and lastname column from both tables and fill in the Customer_id in the customer_id column in the Orders table if there is a match with the firstname and lastname

How can I do this?

Upvotes: 0

Views: 80

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270493

You can use a correlated subquery:

update orders
    set customer_id = (select customer_id from customers c where c.firstname = orders.firstname and c.lastname = orders.lastname)
    where customer_id is null;

Upvotes: 0

Related Questions