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;
Related
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.
I am using mysql+php. The php provides interface to import XLSX table of goods into Mysql table(s). I am using a temporary table created with CREATE TABLE LIKE i.e. empty clone of live table for user evaluation before it is merged with live table
I am using INSERT INTO finalTable SELECT * FROM importTable ON DUPLICATE KEY UPDATE .... to avoid INSERT for every record. The fields to be updated on duplicate key are determined by php with SHOW COLUMNS i.e. all columns from the temporary table.
My problem is that the XLSX does not necessarily contain ALL the fields(name, price, category) i.e. it may be just an update of existing records. The current method will update ALL fields no matter if they were set or not during the import i.e. if the XLSX contained only price update, the rest of fields is null and would overwrite current values.
I tought to alter the temporary table by removing columns(not rows!) that are null for all rows. This way the SHOW COLUMNS would return only update-able columns. If a fields needs to be zeroed, it would be easy to set a special value e.g. "!X!" and use single UPDATE sentence to do so.
Is there a method for this or do you have any better suggestion(I am open to abando ON DUPLICATE KEY too)?
I'm a bit confused by your question.
I am using INSERT ... ON DUPLICATE KEY UPDATE to avoid INSERT for every record.
Like it says, it inserts or updates, you don't avoid anything. If you want to avoid inserts use INSERT IGNORE.
If I were you, I'd merge the tables in two steps.
First step, the insert (just the necessary ones):
INSERT INTO finalTable (col1, col2, ...)
SELECT i.col1, i.col2
FROM importTable i
LEFT JOIN finalTable f ON i.ID = f.ID
WHERE f.ID IS NULL;
Second step, the update:
I don't understand, why you want to delete columns. Unnecessary step which might take a while, and most importantly, your problem of updating with NULL still persists if just a few rows in a column are NULL in your import table. So, here's the solution.
UPDATE finalTable f
INNER JOIN importTable i ON f.ID = i.ID
SET f.col1 = COALESCE(i.col1, f.col1),
f.col2 = COALESCE(i.col2, f.col2),
...;
The trick in the second step is, to use COALESCE(). This function returns the first of its parameters which is not null. So if you have a column which is null in your import table, the value in the final table stays as it is.
UPDATE:
If you insist on having just one statement, you can of course do
INSERT INTO final f
SELECT * FROM import i
ON DUPLICATE KEY UPDATE
SET f.col1 = COALESCE(i.col1, f.col1),
f.col2 = COALESCE(i.col2, f.col2),
...;
If I have two tables in a MySQL database that both have a column called order_number, given an order_number value but not knowing which table it comes from how would I go about setting up a query that would return the name of the table it was found in?
I am particularly interested in the name of the table so I can set up subsequent updates to that table.
Also, I am using PHP for the handling of the query.
select "tableA" as tableName,order_number from tableA where order_number=5
UNION
select "tableB" as tableName,order_number from tableB where order_number=5;
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
There are two tables called table1 and table2
both table has got same index. when updating table1 and table2 if table2 hasnt got a data and table1 has got the same then it should insert that data to table2. so that both tables got the data and is updated. how can I do this??
create a trigger on table 1 to query for the new row in table 2. if not found, insert.
showing code would help, but one option is to use REPLACE() it will add if needed to replace otherwise, if you really need two identical tables you should look in to replication and stored procedures