I have two database table.Two table have same structure. Now i want to insert data from old table to new table if data already exists it will update the old data otherwise insert new. I want to insert or update data by matching some column field value. Any help?
You can make use of ON DUPLICATE KEY UPDATE feature of MySQL. From MySQL doc -
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that
would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL
performs an UPDATE of the old row.
So, if you have keys defined in the tables you can use this feature. For example, your statement will look like -
insert into target_table (col1, col2, ...)
select col1, col2,... from source_table
on duplicate key update
col1 = values(col1),
col2 = values(col2),
The Best way is you can use the left outer join concept.That will be easy.
INSERT INTO table1
(col_1, col_2, col_3, col_4, col_5) values("","","","")
SELECT
table2_col_1,
table2_col_2,
table2_col_3,
table2_col_4,
1
FROM
table_2 AS t2
LEFT OUTER JOIN
table1 AS t1
ON
t1.col_1 = t2.table2_col_1;
UPDATE table_2
SET table2_col_1 = 'value'// here is the value that you need to implement
WHERE t1.col_1=t2.table2_col_1;//here is your condition
From your Question , i understood that your table2 is not that much important.
So you can drop the values present in the entire table2 ,so that the structure will not get affected.After that was finished.. you can just export the insert query to implement the values that is present in the table1.
Related
I have a little problem. I've create a two tables 'kontrola' and 'naruszenie'. In kontrola table I have a foreign key relation to 'naruszenie' table. When I want to display all records belong to 'kontrola' table I use this:
$listakontroli = $connecting->query("SELECT k.id, k.podmiot, k.miasto, k.wszczeto, k.zakonczono, n.naruszenie FROM kontrola k INNER JOIN naruszenia n ON k.naruszenie_id=n.id");
And everything working properly.
How to create a query INSERT TO to add the new record using this 2 tables?
EDIT:
Now I have query like this but doesn't work
$dodajKontrole = "INSERT INTO kontrola (podmiot, miasto, wszczeto, zakonczono, naruszenie_id) VALUES ('$nowyPodmiot', '$noweMiasto', '$datawszczecia', '$datazakonczenia', '$nowenaruszenie')";
Try to use a mysql TRIGGER if you want to do it in mySQL level
CREATE TRIGGER insert_into_naruszenia AFTER INSERT ON `kontrola`
FOR EACH ROW
BEGIN
INSERT INTO naruszenia (naruszenie) VALUES (NEW.id)
END;
INSERT INTO kontrola (....) VALUES (....);
I didn't test it but is should do the job.
If you want to use it inside php code you always have the LAST_INSERT_ID() that you can use.
INSERT INTO kontrola (....) VALUES (....);
INSERT INTO naruszenie (id) VALUES (LAST_INSERT_ID());
I have two tables say, Table1 and Table2.
Table1 contain 20 fileds with primary key ID for Table1 auto increment and Table2 contain same 20 column with primary key ID1 for TAble2 auto increment.
Table1- ID,Column1,Column2,Column3....,Colunm20.
Table2-ID1,ID,Column1,Column2,.....,Column20
I insert each record into Table1. I want to check duplicate Entry for Table1 if all field are same.
Table2 contain the Entry form Table1.If I modified record First record goes to Table2 as it is then it will update into Table1.(i.e first copy the record into Table2 for backup and then update into Table1) So, same condition for Duplicate entry check near Table2 also need. Please Help!
I have coded this in php.
You can write trigger for table 1 which is copying data from table2 on every insert event.
And for unique data either you can check it on php side before you insert or you can define column as unique. Your question is bit confusing so Its difficult to give exact answer.
What are the unique columns ??
I've a table with the purpose of linking two other tables with foreign keys (table 3).
table1 = id, name
table2 = id, sport
table3 = fk_table1_id, dk_table2_id
I do server side code with php and i need a way to automatic insert a row into table 3 when a row is inserted into table2.
But i dont know how to do a php query that will get the generated id of the new row in table2?(The id of table one i do have stored in a php variable)Im a looking at a smart stored procedure?
Steps to follow:
Insert a row into table2.
Find the generated key from table2 and assign it to a PHP variable.
-- read this into a php variable, say, $last_key
SELECT LAST_INSERT_ID();
Use the $last_key in insert statement for table3.
You can Follow below Steps:
1) Make ID to AUTO_INCREMENT IN Table2
2) Create Store Procedure that work as below:
A) INSERT Value to your Table2 :
B) Increase ID value : SELECT LAST_INSERT_ID() INTO VARIABLE;
C) Insert Value to your Table3 : use VARIABLE to get ID of Table2
How to duplicate a table with keys & other structure features retained? including primary key, foreign keys, and indexes.
Can this be done with a single MySQL query?
I'm using "create table newtable as select..." but this method makes all keys & indexes lost.
duplicating a table from another table (with indexing and structure)cannot be done with a single query
you will need need 2 queries.
1) To create Duplicate table.
CREATE TABLE Table2 LIKE Table1;
This will create an exact copy of the table.
2) Fill in the Duplicate table with values from original table.
INSERT INTO Table2 SELECT * from Table1;
will fill Table2 with all the records fom Table1
you can do it in this query
CREATE TABLE a LIKE b
after you can insert
INSERT INTO a SELECT * FROM b
read more information in this article
Following query creates and duplicates data.
CREATE TABLE table2 SELECT * FROM table1
I have two tables in my database, table1 and table2. They are identical. But sometimes I change the data in table1.
How do I copy the data from table1 and update table2 to look the same?
I tried REPLACE INTO table2 SELECT * FROM table1 but it works like INSERT and just make new rows instead of updating the existing ones.
For REPLACE INTO to work as intended, the destination table must have a primary key defined, otherwise MySQL cannot determine whether a row already exists and always assumes a new row. As a result, for tables without a primary key, REPLACE INTO acts exactly like INSERT INTO.
Alternatively, you can use two queries, one UPDATE and one INSERT, with appropriate WHERE (NOT) EXISTS clauses. The advantage of this is that it's portable (REPLACE INTO is a MySQL extension).
Another alternative is to run two commands...
truncate table table2;
insert into table2 select * from table1;