I'm struggling with joining two tables with MySQL.
The first table is a subset of customers and is called "merge1", and contains the following records:
customer_id First Name Total Spent Total Orders
==========================================================
1 Mike 0.00 0.00
2 Allan 0.00 0.00
3 Craig 0.00 0.00
4 Michael 0.00 0.00
Table 2 has sales data and is as follows and is called "merge2", and contains the following records:
customer_id Total Spent Total Orders
=========================================
1 65.00 1
2 64.00 1
3 109.00 1
4 65.00 1
I'm trying to update the "Total Spent" and "Total Orders" in the first table (merge 1) by runninng the following SQL:
INSERT INTO `merge1` (`customer_id`, `Total Spent`, `Total Orders`)
SELECT
merge2.`customer_id`,
merge2.`Total Spent`,
merge2.`Total Orders`
FROM
merge2
JOIN
merge1 ON merge2.customer_id = merge1.customer_id;
The query doesn't error out but the table isn't updating.
Can someone point me in the right direction, I'd really appreciate your help!?
Answer
Thanks heaps for the comments below! I should have used the "Update" command. Here is the syntax I used if it comes in handy for others.
UPDATE merge1 T1 JOIN merge2 T2
SET T1.`Total Spent` = T2.`Total Spent`, T1.`Total Orders` = T2.`Total Orders`
WHERE T1.customer_id = T2.customer_id;