I have two tables. table1 (source) and table2 (destination). I want to whole records in table2 to be copied to table1 if and only if the record is not already exists in table1. I use the following statement:
INSERT INTO test.dest
SELECT *
FROM test.source
WHERE NOT EXISTS(SELECT *
FROM test.dest
WHERE (source.DomainName=dest.hostnmae)
);
My question: Is there another better method to speed the process??
Related
How it's possible to copy one entry row of a table with same data to another (same ID, same data values) Database -> same table?
Example:
Table Units:
UID Department Name Item
67 HR John Doe Table
If both tables equal no. of columns and in same order you want to insert then just use below simple query-
INSERT INTO mytable SELECT * FROM units WHERE uid=67;
If you want to insert selected column in another table's selected columns and in your order then use below-
INSERT INTO mytable(col1,col2,col3,col4) SELECT uid,department,`name`,item FROM units WHERE uid=67;
If I understand you correctly you want to copy some rows to table of another DB.
Try INSERT SELECT Query:
insert into db1.tbl(id,col1,col2)
select id,col1,col2 from db2.tbl;
Use trigger option in mysql to make new table with same data.
Suppose if you want to copy table1 data to table2 with some condition.
INSERT INTO table2 (ID, NAME) SELECT Col1, Col2 FROM table1 WHERE Col1='<Your_Condition>';
Here table2 have fields like ID and NAME and table1 have fields like Col1 and Col2.
In that case, above query copy table1 data to table2 on these fields where condition matched on table1, if you want to copy whole data of table1 then remove the WHERE condition from Select Query.
I am trying to move a complete row of records which includes BLOB from one table (TABLE1) to another table (TABLE2) in the same database using SQL. I have tried what little I know but it failed.
What I have already used which failed is:
INSERT INTO TABLE2
SELECT * FROM TABLE1
WHERE
staff_id = '0010002';
How should I do this instead?
Try:
INSERT INTO TABLE2 (colname1, colname2)
SELECT colename1, colename2
FROM TABLE1
WHERE staff_id = '0010002'
And for more details.http://dev.mysql.com/doc/refman/5.0/en/insert-select.html
How can we copy data from one table into another table which doesn't exist in the first one. In table one of the column is primary key.
INSERT INTO table SELECT * FROM db2.table;
ERROR 1062 (23000): Duplicate entry '100001' for key 'id_UNIQUE'
You can use the predicate NOT IN to do so like this:
INSERT INTO table1
SELECT *
FROM db2.table2
WHERE table1ReferenceID NOT IN(SELECT id_UNIQUE FROM table1);
This will checks whether this table1ReferenceID found in the first table or nor. Therefore, the SELECT clause will select all the rows from the second tables except those that is already presented in the first table table1.
Note that: the column table1ReferenceID is the reference of the id_UNIQUE in the second table.
Other alternatives for this, is to LEFT JOIN as suggested by #HamletHakobyan's answer and NOT EXISTS.
See it in action
Try this:
INSERT INTO table
SELECT T1.*
FROM db2.table T1
LEFT JOIN table T2
ON T1.Id = T2.Id
WHERE T2.Id IS NULL;
WHERE [primary_key_table1] not IN (SELECT [primary_key_table2] FROM [table2])
so i suggest something like
INSERT INTO table (SELECT * FROM db2.table WHERE id NOT IN (SELECT id FROM table));
I'm trying to update or insert data dependent on whether the account number is already in the existing data.
Firstly I added the new variables to those with an acocunt number already in the table using this
drop table #test1
select a.*, B.Delq_Sep12, b.Bal_Sep12, b.Queue_Sep12
into #test1
from pcd1 a
left join #pcd_sep12 b on (a.ACCOUNT_NUMBER = B.account_number)
Then I add all those records whose account number is not in test1 (created above) from #pcd_sep12 into test1
INSERT #test1
SELECT * FROM #pcd_sep12 WHERE account_number NOT IN(SELECT account_number FROM #test1)
I get the error Column name or number of supplied values does not match table definition.
I realise its because theres not the same number of fields but is there a way around this?
Why not use the MERGE (aka "upsert") statement?
MERGE INTO pcd1 M
USING (SELECT * FROM #pcd_sep12) src ON M.account_number = src.account_number
WHEN MATCHED
-- UPDATE stuff
WHEN NOT MATCHED BY TARGET
-- INSERT stuff;
This way you don't need a temp table or any tests: these won't be concurrency-safe under load
You have to specify the columns like this
INSERT INTO #test1 (column1, column2, column3)
(SELECT column1, column2, column3 FROM #pcd_sep12 WHERE account_number NOT IN(SELECT account_number FROM #test1))
if i am not mistaken, your #test1 table has columns from pcd1 table also. but in your second query, you are only selecting from #pcd_sep12.
Sometimes if I want to quickly copy records from one table to another (that has the same structure) I use a query like this:
INSERT INTO table2 SELECT * FROM
table1 WHERE id = SOME_VALUE
How can I add a ON DUPLICATE KEY UPDATE to this statement? I tried this:
INSERT INTO SELECT * FROM table1 WHERE
id = 1 ON DUPLICATE KEY UPDATE SELECT
* FROM table1 WHERE id = 1
But I get an error. Is there away to accomplish the query above with out individually listing each column in the query?
P.S. Yes, I realize that it is not good practice to have multiple tables with identical structures, but sometimes you just don't get control over everything in the workplace!
The below UPDATES if there is no PK duplication and INSERTs is there is:
REPLACE INTO table2(field1, field2, field3)
SELECT field1, field2,field3 FROM table1
WHERE id=1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html Just use the SELECT field_name from the other table like in dnagirls example