I want to update a column from one table to the column in another table. Both columns have the same name and same ID and are populated. I need to update only IDs which match in both tables ( column id)
So for example all values from table_1 in column_x to be copied to table_2 in column_x if both are with same column_id
Fixed with help from other site.
tried: UPDATE table1 JOIN table2 ON column1.column_x = table2.column_x SET table2.id = table1.id;
It looks like you've mixed up which column to join on and which column to copy. Your stated problem is:
all values from table_1 in column_x to be copied to table_2 in column_x if both are with same column_id
The query to do this would be the following:
UPDATE table1 JOIN table2 ON table1.column_id = table2.column_id
SET table2.column_x = table1.column_x;
Or slightly more concisely:
UPDATE table1 JOIN table2 USING (column_id)
SET table2.column_x = table1.column_x;
Related
I have an empty table (t1) and I want to insert or update the t1.uid column from another table's (t2) GROUP BY uid values.
So far I have tried like this:
UPDATE table1 t1 JOIN
(SELECT uid FROM table2 GROUP BY uid) t2
SET t1.uid = t2.uid;
but it's not working for me.
N.B. I've got a massive data set for which group by (uid from table-t2) results giving me total 1114732 results which I have to insert/update in t1 table's uid column.
Please try this:
Insert into table1(uid)
select distinct uid from table2
If table1 is empty, then UPDATE is not the correct verb. Would this suit your needs?
INSERT into table1 SELECT distinct uid from table2;
INSERT ... SELECT docs
I have a table which contains unique names, call it table1. I have another table which contains the same names but each name occurs several times, call it table2. Now, I want to copy data from table2 to table1 corresponding to the names. And if table2 has multiple records by the same name, I want the corresponding new records to be created in table1.
TABLE1 TABLE2
NAME NAME
A A
B A
C B
D B
Following the little chat int he comments, you could try this:
UPDATE t1
set columnx = t2.columnx
FROM table1 t1
LEFT JOIN table2 t2 on t2.name = t1.name
WHERE t2.name is null
To achieve your full requirements, you may find it more useful to have multiple queries accomplish the one task.
Need help with a mysql query. I have 2 tables - table1 and table2. I am trying to update a field in table1 which is not included in table2 - named Status. And I want to update that field with the value of 'A'. table1 and table2 DO have a field in common - named Member_ID. Here is my query that is giving me errors:
UPDATE table1 SET Status='A' WHERE Member_ID=table2.Member_ID;
Is there some type of join that is needed? Any help would be appreciated. Thanks.
cdr6800
This can be done with exists
UPDATE table1 s
SET s.status = 'A'
WHERE EXISTS(select 1 from table2 t
WHERE t.member_id = s.member_id)
You have to JOIN table1 to table2 like this:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.Member_ID = t2.Member_ID
SET t1.Status='A'
This will update all table1 records having a Member_ID value that also exists in table2.
I have two almost identical database tables with the presice same colummns and number of rows in them.
They both have a unique ID per row which is the same in both tables.
The first table is missing some values on some columns.
How can i copy values from a row in table2, to row in table1?
The columns have the same name, but the value is empty in table1, but not table2. I want to copy over all values from some columns in table2 to same columns in table1.
It's a big table with over 1 million rows.
Example:
Table 1 Row 5:
id = 5
orgnr = 932942
homepage = NULL
name = NULL
Table 2 Row 5:
id = 5
orgnr = 932942
homepage = 'www.domain.com'
name = 'John Smith'
I want to copy values from homepage and name to table1's columns.
Thanks in advance.
In MySQL you can use joins to update a table with values from another table:
UPDATE table1 t1
JOIN table2 t2
ON t1.id = t2.id
SET t1.homepage = t2.homepage,
t1.name = t2.name
Dirty custom query that requires you to add all fields but could do the trick:
UPDATE table1
SET
field1 = ISNULL(t1.field1, t2.field1),
field2 = ....
FROM
table1 t1
INNER JOIN table2 t2 ON t1.Id = t2.Id
If updating the 1m rows in one go is too much you can try to do it in batches by using a where clase:
WHERE
t1.Id BETWEEN #batchStart AND #batchEnd
I have two tables that I need to import data from where both share a column with the same data. However one table has a value in it that needs to be copied to another. I need an sql command that works like below:
Update table1 contenttitle with table2 title where table2 id equals table1 contentid
So essentially it is copied the values from table2 title and inputting them in table 1 contenttitle where the values in table2 id and table1 contentid are equal.
update table1
join table2 on table1.contentid = table2.id
set contenttitle = table2.title
Update table1
set table1.content1=table2.content2
from table2 join table1 on table2.contentid=table1.contentid
update table1
set table1.contenttitle = table2.title
FROM table1
inner join table2 on table1.id= table2.id