Multiple column update between two table with 'SET' in MySQL - mysql

I have two tables as below.
Tabel A:
ResultID(PK) | ImportDate | Comment1
-------------------------------------
101 | 25-09-2019 | One
--------------------------------------
102 | 25-09-2019 | Two
--------------------------------------
103 | 25-09-2019 | Three
----------------------------------------
Table B:
ResultID(PK) | ImportDate | Comment2
-------------------------------------
101 | 26-09-2019 | new one
--------------------------------------
104 | 26-09-2019 | four
--------------------------------------
So the output should look like
Table A:
ResultID(PK) | ImportDate | Comment1
-------------------------------------
101 | 26-09-2019 | new one
--------------------------------------
102 | 25-09-2019 | Two
--------------------------------------
103 | 25-09-2019 | Three
--------------------------------------
104 | 26-09-2019 | four
--------------------------------------
Question:
I want to get resulting Table A as mentioned above if ResultID is matched between Table A and Table B, I want to update all column in Table A from Table B for that ResultID. If ResultID from Table B is not present in Table A, insert it to Table A.
What I have tried in MySQL:
UPDATE TableA
SET comment1=
(SELECT comment2 FROM TableB WHERE TableA.ResultId=TableB.ResultId);
Above solution only works for one column to update Table A. I also tried with multiple column update reference from
SQL Server 2005 implementation of MySQL REPLACE INTO? but multiple column update is not working for my scenarios.
For my real scenarios- I have 40 columns and 50,000 rows.
Could you please provide me any hint or solution? Thank you.

Since you have a primary key on ResultID, you can simply use an INSERT ... ON DUPLICATE KEY UPDATE query to transfer all the data from TableB to TableA:
INSERT INTO TableA (ResultID, ImportDate, Comment1)
SELECT ResultID, ImportDate, Comment2 FROM TableB
ON DUPLICATE KEY UPDATE
ImportDate = VALUES(ImportDate),
Comment1 = VALUES(Comment1);
Output:
ResultID ImportDate Comment1
101 26-09-2019 new one
102 25-09-2019 Two
103 25-09-2019 Three
104 26-09-2019 four
Demo on dbfiddle

Related

Delete entry from SQL table with connections inside other tables

I've a question. I've this table structure here:
When I have now an issue in my table which has also different labels in my labels table and I want to delete the issue now, do I need to delete the labels before?
I'm planing to delete the entry this way:
DELETE FROM issues WHERE issue_id = 2;
The entries in my labels table are:
---------------------------------
| id | issue_id | label |
---------------------------------
| 1 | 2 | Apple |
---------------------------------
| 1 | 2 | Orange |
---------------------------------
| 1 | 2 | Banana |
---------------------------------
You should delete entries which reference your issue_id before you can delete it in the base table itself.
First you'll have to delete it from the labels table, then you can delete it from the issues table.

Skipping row for each unique column value

I have a table from which I would like to extract all of the column values for all rows. However, the query needs to be able to skip the first entry for each unique value of id_customer. It can be assumed that there will always be at least two rows containing the same id_customer.
I've compiled some sample data which can be found here: http://sqlfiddle.com/#!9/c85b73/1
The results I would like to achieve are something like this:
id_customer | id_cart | date
----------- | ------- | -------------------
1 | 102 | 2017-11-12 12:41:16
2 | 104 | 2015-09-04 17:23:54
2 | 105 | 2014-06-05 02:43:42
3 | 107 | 2011-12-01 11:32:21
Please let me know if any more information/better explanation is required, I expect it's quiet a niche solution.
One method is:
select c.*
from carts c
where c.date > (select min(c2.date) from carts c2 where c2.id_customer = c.id_customer);
If your data is large, you want an index on carts(id_customer, date).

Update query using select -- how does it know which row to update

I have the following query that is working correctly but I don't understand why. I am changing the balance column for each row..How does it know which row to update for a particular customer.
UPDATE phppos_customers SET balance =
IFNULL((SELECT SUM(transaction_amount)
FROM `phppos_store_accounts`
WHERE deleted = 0 and customer_id = phppos_customers.person_id), 0);
For the sake of example, lets say you have the following two tables
[Users]
+---------+----------+
| user_id | username |
+---------+----------+
| 1 | patrick |
| 2 | chris |
+---------+----------+
[Names]
+---------+------------+-----------+
| user_id | first_name | last_name |
+---------+------------+-----------+
| 1 | Patrick | Stewart |
| 2 | Chris | Angel |
+---------+------------+-----------+
If you had a update query like the one in your original post, you would want to tell it how to align the two tables. If you had the clause WHERE Users.user_id = Names.user_id, you are effectively telling SQL to view the data as if both tables were aligned side by side, using the user_id in both tables to determine where they match up. This would mean the first_name and last_name in the [Names] table for user_id 1 will be what is used when updating the row in the [Users] table that is user_id 1. It is essentially viewing the data merged together, like this:
[Users] and [Names] tables aligned by the user_id columns
+---------+----------+---------+------------+-----------+
| user_id | username | user_id | first_name | last_name |
+---------+----------+---------+------------+-----------+
| 1 | patrick | 1 | Patrick | Stewart |
| 2 | chris | 2 | Chris | Angel |
+---------+----------+---------+------------+-----------+
So when SQL is doing the updating, it updates each row with the corresponding data from the other table, using this aligning to know which data to use for each rows update.
The process of aligning/merging data from multiple tables is called joining in SQL, here is some more information that illustrates how it works if you are interested.

Access 2010 update query based on a conversion table

Is there any way that I can, in a single statemente, update the contents of a table based on a conversion table?
For example, if I have the following table called MyStuff
Key | Values
----+-------
1 | Apples
2 | Oranges
3 | Bananas
And supose I have the following ConversionTable
Old Key | New Key
--------+--------
1 | 101
2 | 202
3 | 303
What I'm looking for is an update SQL statement that, based on ConversionTable, would produce the following changes in MyStuff
Key | Values
----+-------
101 | Apples
202 | Oranges
303 | Bananas
Assuming A.[Key] is not an autonumber, this should work:
Update MyStuff A
INNER JOIN Conversion B
on A.Key = B.[Old Key]
Set A.[Key] = B.[New Key]

migrate data from one table to another with parent child association

I am migrating data from table A to table B.
Table A has fields id, parent_id,title, credit.
Table B has fields id, parent_id, title, credit. where id is auto incremental field.
Table A has self association, where parent_id refers to a row in Table A itself.
From a rake task I need to migrate data from table A to table B.
sample data in table A:
id | parent_id | title | credit
12 | nil | ABC | 1
13 | 12 | XYZ | 1
14 | 12 | PQR | 0
15 | 13 | NOP | 1
after migrating data to table B, it should be like this:
id | parent_id | title | credit
1 | nil | ABC | 1
2 | 1 | XYZ | 1
3 | 1 | PQR | 0
4 | 2 | NOP | 1
When migrating data from table A to table B using ruby script, I can update title, credit with new id. How can I update parent_id?
Thanks for the support.
Update parent id after create. Something like this,
a = A.all
a.each { |v|
B.create!({parent_id: v.parent_id,title: v.title,credit: v.credit})
A.update_all({parent_id: B.last.id},{parent_id: v.parent_id})
}
I think so you need to update your ids as well.
table_b_obj.id = table_a_obj.id
table_b_obj.save!
table_b_obj.reload