...

View Full Version : Copying unique row table to another table



Anishgiri
07-07-2011, 02:58 AM
Suppose I have table1 and table2, table2 is exact duplicate of some table rows in table1. When table1 get new record, how can I insert the new record on table2. I know you can use the insert to put the new record of table1 to table2. But what I want is to check table1 if there are records that is not on table2, and if it does, insert that record on table2.

To make it more clear table1 one has fname, lname and country fields and table2 is the duplicate of table1, but table2 does not have the country fields. What I want is if table one get new record, the new fname and lname record will be also inserted in table2.

Below is q query i just not now how to continue it.

SELECT fname,lname
INTO table2
FROM table1
WHERE fname

guelphdad
07-07-2011, 03:17 AM
Table 2 is redundant. you have the information you need in table one. If you only need first and last name query those, if you need country too then query it.

sunfighter
07-07-2011, 06:39 PM
The easiest way to do the two things you want:

I want is to check table1 if there are records that is not on table2, and if it does, insert that record on table2.

A work around - backup table2 or if you don't know how rename it

RENAME TABLE table2 TO table3;

Then create a new table2

CREATE TABLE table2 LIKE table3;

Then populate it with the records of table1

SELECT fname,lname INTO table2 FROM table1;

=========================================

Next question

What I want is if table one get new record, the new fname and lname record will be also inserted in table2.

Find the place in the code where a new record is inserted to table1 and add a insert to table2. This will keep you up to date from this point on.


INSERT INTO table1 (fname, lname, country) VALUES ("First_name", "Last_name", "Living_place");
INSERT INTO table2 (fname, lname) VALUES ("First_name", "Last_name");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum