I want to insert data from a table WorkTableA to another table TableA, without duplicating the data (i.e. do not insert into WorkTableA if the customer name already exists).
Is there a way of doing it through VBA code.
The field name and their properties in both tables are identical.
What you need is an INSERT INTO statement
INSERT INTO WorkTableA
( CustomerName, Col2, Col3...)
SELECT CustomerName, Col2, Col3
FROM TableA LEFT JOIN WaorkTableA ON TableA.CustomerName = WORKTABLEA.CustomerName
WHERE WorkTableA.CustomerName IS NULL
Something like this might work.
The SELECT part of the statement will select only the ones that DO NOT EXIST in WorkTableA
Related
I need to create an identical copy of many records in a table. The table has a PK id, which of course will be different, in the freshly-copied records.
For example, let's say i have a scrum_card table, with the following non-unique columns: name, description, board_id
I have a dynamic array of id's of records, which i wish to duplicate: [34,56,32,3445,...]
How do i tell MYSQL, to fetch the data from all those records, and make a batch-insert of those same records?
In "human" syntax it would look something like this: "Select all columns(besides id) from scrum_card where the id's are [array of id's], then duplicate each found record".
Use INSERT INTO ... SELECT... and in the list of columns do not include the id:
insert into scrum_card(name, description, board_id)
select name, description, board_id
from scrum_card
where id in (34,56,32,3445,...)
You can use INSERT using a SELECT result set as the source, instead of a set of literal row tuples with VALUES(...).
INSERT INTO new_table (id, col1, col2, col3...)
SELECT NULL, col1, col2, col3...
FROM old_table
WHERE id IN (34,56,32,3445,...)
Using NULL in place of the id column in the SELECT will return NULL for each row, which will cause new_table to generate a new id value.
But SQL does not have any way to do a wildcard like "all columns except id," besides you typing the column names in.
I am trying to copy data from one table to another with an additional value.
from table1 I need to copy two column(field) values such as ERNO and ENAME to table2.
Also need to update or add ECNO(column or field).
Note: I am using MySQL.
Not only one field ECNO, also need to add more field while copying data from one table to another.
ECNO field is int datatype.
Follwing query I have used for that. But it doesn't work
INSERT INTO TABLE2 (ECNO, ERNO, ENAME) values (1, select ERNO, ENAME from TABLE1)
Any Suggestion how to do this in proper way.
Use INSERT INTO...SELECT syntax:
INSERT INTO TABLE2 (ECNO, ERNO, ENAME)
SELECT 1, ERNO, ENAME
FROM TABLE1
Use like this:
INSERT INTO tbl2 (col1, col2.....)
SELECT tbl1.col1,tbl1.col2
FROM tbl
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 working on redesigning of a legacy db and I have set new names to columns of old db. So, for instance, if olddb.oldtable under dbold has column descr, I have set it as description in new newdb.netable for column.
How can I mention individual columns in my query?
I am using MYSQL
Update: Both Databases are on different IP Addresses and I am using Navicat to transfer data.
You can try like this:
INSERT INTO newtable (col1, col2, ..., )
SELECT col1, col2, ..., FROM oldtable
By trying the above query you can insert the specific column. So for example if your newtable has a column as description and old table as descr then you can mention it like:
INSERT INTO newtable (col1, col2, `description`, ..., )
SELECT col1, col2, `descr` ,..., FROM oldtable
Also if the table column list is large and you want to copy all the columns and its data then you can simply use the wildcard charater * as:
INSERT INTO newtable
SELECT * FROM oldtable;
You can insert all columns at once without the need to mention the names using this:
INSERT INTO newtable (SELECT * FROM oldtable);
It will make an 1x1 match independently of column names.
If types don't match then will insert default values (not checked for all the type combination).
Note that column number must be the same on both tables otherwise an error like this will occur:
#1136 - Column count doesn't match value count at row 1
I have a table that contains some duplicate redords. I want to make records unique. I created a new table (say, destination) and I specified a unique column in it. How can copy records from table1 (source) such that, if the record inserted in the destination table, it does not insert it again.
You can use the "select into" construct and select insert only distinct rows, like this:
insert into table_without_dupes (column0, column1) select distinct column0, column1 from table_with_dupes
If you have autoincrement or other columns that makes the rows distinct, you can just leave them out of the insert and select parts of the statement.
Edit:
If you want to detect duplicates by a single column, you can use group by:
insert into table_without_dupes (column0, column1) select column0, column1 from table_with_dupes group by column0
MySQL will allow you to refer non-aggregated columns in select, but remember that the documentation says "The server is free to choose any value from each group", if you want to select one specific row of the groups, you might find this example useful.
Generic approach
insert into destination(col1,col2)
select DISTINCT col1,col2 from source as s where not exists
(select * from destination as d where d.col1=s.col1)
Edited
insert into destination(col1,col2)
SELECT distinct col1,col2 from source
Edited (Assuming col3 is duplicated and you want only one copy of it.)
insert into destination(col1,col2,col3,....,colN)
SELECT col1,col2,col3,...,colN from source as s1 inner join
(
select col1,col2,max(col3) as col3
from source
group by col1,col2
) as s2 on t1.col1=t2.col1 and t1.col2=t2.col2 and t1.col3=t2.col3
insert into <dest_table_name>
select distinct * from <source_table_name>;