I have two different tables with two colums that have the same name, datatype and size.
My issue:
When I try to copy the content from one column to the other,
update Table1
set Column4 = (select Column1 from Table2);
I get an error. (subquery returns more than one row)
My question:
Is there a way for me to copy the content from Table2 to Table1 in a similar fashion as the code shows above?
Use Insert ... Select
Insert INTO Table1 (Column4)
SELECT Column1 FROM Table2
Related
I use this site all the time to find answers to my questions but this is the first time I've ask one.
I have two tables and want to query both tables and insert the results into two columns on table 1. Something like this:
SELECT a.column1 from table1 a LEFT Join ( SELECT 'column1' from 'table2' ) AS a ON where a.column1 like '%column1.table2%';
Basically then insert the result into column5 and column6 on table 1
I know that this isn't correct as it doesn't work and it's not going to update any thing. For testing I'm running select statements to verify before running the update command. Another way of saying what I need would be:
If column1 in table1 is like column1 in table2 then update column5 in table1 with corresponding entry from column2 in table2 and update column6 in table1 with column7 from table1 with corresponding entry in column3 from table2;
I realize that this is not the best explanation but that is the best way I can explain what I want. Please ask questions if more information is needed and I will do my best to explain.
Thanks for any input you have.
In MySQL you can do multi-table updates in a single update statement using joins in the table list:
update t1 inner join t2 on t1.column1 like concat('%',t2.column1, '%')
set t1.column5=t2.column2, t1.column6=t2.column7
However, using like is not necessarily the best idea, since more than 1 records from t2 may match the same record within t1, therefore a single t1 record may be updated several times during a single run.
Your description of which t1 fields should be updated by which field from t2 is inaccurate, cannot really tell the logic there.
How it's possible to copy one entry row of a table with same data to another (same ID, same data values) Database -> same table?
Example:
Table Units:
UID Department Name Item
67 HR John Doe Table
If both tables equal no. of columns and in same order you want to insert then just use below simple query-
INSERT INTO mytable SELECT * FROM units WHERE uid=67;
If you want to insert selected column in another table's selected columns and in your order then use below-
INSERT INTO mytable(col1,col2,col3,col4) SELECT uid,department,`name`,item FROM units WHERE uid=67;
If I understand you correctly you want to copy some rows to table of another DB.
Try INSERT SELECT Query:
insert into db1.tbl(id,col1,col2)
select id,col1,col2 from db2.tbl;
Use trigger option in mysql to make new table with same data.
Suppose if you want to copy table1 data to table2 with some condition.
INSERT INTO table2 (ID, NAME) SELECT Col1, Col2 FROM table1 WHERE Col1='<Your_Condition>';
Here table2 have fields like ID and NAME and table1 have fields like Col1 and Col2.
In that case, above query copy table1 data to table2 on these fields where condition matched on table1, if you want to copy whole data of table1 then remove the WHERE condition from Select Query.
Im trying to use insert and select in one query.Both the tables have same number of columns except for one column in the table where data is to be inserted
table2 is the mirror image of table1 except for a column called comments;
insert into table2 select * from table1 where city='XYZ' and name = 'STY'
since the no of columns arent equal i get the following error:
Column count doesn't match value count at row 1
INSERT INTO table2 (Coln1,Coln2,Coln3,....) SELECT * FROM table1 WHERE `city`='XYZ' AND `name`='STY';
For the extra column in the table2, NULL is set by default.
I want to move one or two rows(with data) for e.g. from end to first position or from middle to end? With phpmyadmin with GUI there isn't option to moving rows.
Another question:
How to move one table to another table by copying data?
Rows position in a resultset are determined by ORDER BY clause or by "chance" if it's not specified, so moving from a position to another in absolute has no meaning.
You can use INSERT SELECT statement to copy data from table1 to table2 if they have the same structure.
INSERT INTO table2
SELECT *
FROM table1
You may copy data with help of a query:
INSERT INTO table2 (col1, col2, col3) SELECT col1, col2, col3 FROM table1
I want to insert column1 from table1 to column1 in table2. If the value at column1 in table2 already exists I don't want it to insert it.
Though, I found a question on here that is similar but with all the table columns/rows instead of just one, plus both tables have different schemas except for column1. Because of this, I thought this question would still be valid to post for a more specific answer for mysql newbies like me.
Insert Table2( Column1 )
Select Column1
From Table1 As T1
Where Not Exists (
Select 1
From Table2 As T2
Where T2.Column1 = T1.Column1
)