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
Related
I would like to know how I can insert new rows in my table 1 of table 2. The idea is that by comparing the two tables if in the second one you do not find the same ID in table 1 this inserts the new data in table 1.
This is the two table and the idea I want to do:
Tabla 1
ID-Name-Surname
1-Oriol-Molina
Tabla 2
ID-Name-Surname
1-Oriol-Molina
2-Ricard-Martin
And the result would be this:
Tabla 1
ID-Name-Surname
1-Oriol-Molina
2-Ricard-Martin
Tabla 2
ID-Name-Surname
1-Oriol-Molina
2-Ricard-Martin
Use the database to enforce data integrity. That is, if you don't want duplicate ids in the table, then declare a unique index/constraint:
create unique index unq_table1_id on table1(id);
Then, in MySQL, you can use on duplicate key update:
insert into table1 (id, name, surname)
select id, name, surname
from table2
on duplicate key update id = values(id);
The final statement is a no-op -- it does nothing except prevent an error.
The advantage of this approach is that the database will ensure that id is unique for any statement that inserts data into the table, not just this one.
You can use INSERT INTO .. SELECT with LEFT JOIN and IS NULL check, to fetch only those rows from Table2 which do no exist in Table1
INSERT INTO Table1 (ID, Name, Surname)
SELECT t2.ID, t2.Name, t2.Surname
FROM Table2 t2
LEFT JOIN Table1 t1 ON t1.ID = t2.ID
WHERE t1.ID IS NULL
You can try using a left join
insert into table1
select id, name, surname from table2 left join table1 on table2.id=table1.id
where table1.id is null
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
I have four tables having same structure. Say they have a column name in them. I need to check if values in column name of table1 is present in column name of table2.
If present, I need to insert this name in column name of table3, else in column name of table4.
Assuming INSERT and Postgres ...
I would use a data-modifying CTE (Postgrs 9.1+) with one SELECT and two INSERT:
WITH cte AS (
SELECT t1.name, t2.name IS NULL AS t2_missing
FROM table1 t1
LEFT JOIN table2 USING (name)
)
, ins AS (
INSERT INTO table3 (name)
SELECT name FROM cte WHERE NOT t2_missing
)
INSERT INTO table4 (name)
SELECT name FROM cte WHERE t2_missing;
The same is not possible in MySQL (no CTEs, not to mention writable CTEs).
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);
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