insert all from table1 to table2 having different table structure - mysql

table1 have columns id what title and five columns more;
table2 have only columns id what and title;
How to insert all rows from from table2 into table1?
insert into table1 select * from table2 - doesn't work because of different table structure.

You can try below -
insert into table1(id, what, title)
select id, what, title from table2

Related

How to insert multiple rows using insert while selecting data from another table

insert INTO table1 (price,service_id,country_id)VALUES(2.00,(SELECT id from table2), (SELECT id from table3))
It shows error.
Table 1-
id, price, service_id, country_id
Table 2-
id, service_name
Table 3-
id, country_name
When inserting in mysql using select query, you don't need the values() anymore.
Try this:
insert INTO table1 (price,service_id,country_id) Select t2.ID, t3.ID from table_2 t2, table_3 t3
Here is one example.

How to merge two tables with same values in a column

I have two tables, Table1 and Table2.
Table1 has the columns "ID","Date" and Table2 has the columns "ID", "Cost".
Now, the way I want to merge these two tables into a new table Table3 with columns "ID", "Date", "Cost" is that the Cost and Date are in the same row which have the same ID in both Table2 and Table1 respectively.
In short, I want to glue the two tables with respect to a column, in this case "ID".
I've looked into statements like INSERT INTO TABLE but I haven't been able to get it to work.
You can perform an insert-select on the result of the join between the two source tables:
CREATE TABLE table3 AS
SELECT table1.id AS id, date, cost
FROM table1
JOIN table2 ON table1.id = table2.id
please use below SQL to merge
insert into table3 (id, date1, cost)
select a.id, b.date1, a.cost from table1 a, table2 b where a.id=b.id;

mysql merging two tables if not exist

I want to merge two tables, but not the duplicate entries with similar id field.
But I get error after:
INSERT INTO table1 (id, name)
SELECT id, name FROM table2 WHERE table2.id NOT_IN (SELECT id FROM table1);
You have to change NOT_IN in NOT IN, as this is the correct synthax
INSERT INTO table1 (id, name)
SELECT id, name FROM table2 WHERE table2.id NOT IN(SELECT id FROM table1);

Mysql 3 tables, copy column

I have three mysql table from same database Db1.
Three tables have following columns.
Table 1:
Name
City
Branch
Table 2:
Age
Address
Country
Table 3:
No columns.
I want to copy Table1.Name and Table2.Age to Table 3. How can I do it?
This makes little sense if table1 and table2 can not be joined and table 3 doesn't have the 2 columns. If you can join:
insert into table3 (name, age)
select table1.name, table2.age
from table1 join table2 on (table1.columnToLinkFromTable1 = table2.columnToLinkFromTable2)
You could also do it like this, but of course it doesn't make much sense:
insert into table3 (name, age)
select table1.name, table2.age
from table1, table2

MySql Duplicate-data an entry on another table

I have table1,table2 => all fields are identical except that table2 two has an extra field which is a FK of table1
**table1** *ID*,content,status
**table2** *ID*,content,status,tid
so tid = table1 id.
I need to copy a row from table1 one to table2 so esentially the table2 would be a backup of table1. I can do it using mysql,then php, then mysql again I was wondering if you could have a simpler solution on mysql :)
hope its not too complicated
If you want to copy every row in table1 into table2, you could do:
INSERT INTO table2 (id, content, status, tid)
SELECT id, content, status, id FROM b;
If table2 isn't empty, you could add an ON DUPLICATE KEY... clause to deal with clashes.
EDIT
If you just want to copy one row, you can add a WHERE clause:
INSERT INTO table2 (id, content, status, tid)
SELECT id, content, status, id FROM b WHERE id=123;
INSERT INTO table2 (content, status, tid) SELECT content, status, ID FROM table1