I have two tables calendar and customer table. Calendar table have a "customer" column which has customer table "ID" as value. But unfortunately, this calendar customer field value was wrongly populated with other values. Both tables have these common fields Date, SeatingID and BusID. How to update the calendar table customer field based on these common fields?.
Below is the structure of both tables.
Customer Table
calendar Table
In such a case, you can use the following UPDATE statement syntax to update column from one table, based on value of another table. UPDATE first_table, second_table SET first_table. column1 = second_table. column2 WHERE first_table.id = second_table.
You can update an entire row in one table with values from a row in another table.
You can UPDATE
the Customer
field of the second table Calendar
from the first table Customer
by JOIN
ing the two tables like so:
UPDATE calendar c1
INNER JOIN Customer c2 ON c1.SeatingID = c2.SeatingID AND c1.BusID = c2.BusID
SET c1.Customer = c2.ID --or SET c1.Customer = c2.PassengerName or whatever you want.
In the SET
clause, you can set the column you wish to update, and you can also JOIN
the two tables based on any predicate, I used c1.SeatingID = c2.SeatingID AND c1.BusID = c2.BusID
, but you can choose what is suitable for your needs.
Try this code:
UPDATE calendar cal, customer cust
SET cal.Customer = cust.ID
where cal.SeatingID = cust.SeatingID
and cal.BusID = cust.BusID
and cal.DATE = cust.DateOfTravel;
Here is link to more informations abaout update
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With