I have 2 tables : OSUSR_1SV_STAGING_FTP and #OSUSR_1SV_STAGING_FTP1
with same columns on both the table as :
Customer_Part_Number,
Lear_Part_Number,
Shipping_ID,
Customer_Name,
Effective_Date,
End_Date,Change_ID,
PO_Number,
PO_Price
I have successfully copied all the data to table #OSUSR_1SV_STAGING_FTP1 from table OSUSR_1SV_STAGING_FTP.
But my scenario stucks when the records are same in table OSUSR_1SV_STAGING_FTP and I don't want it to get it inserted to table #OSUSR_1SV_STAGING_FTP1.
Just need to insert non duplicate records.
This inserts records only which are in OSUSR_1SV_STAGING_FTP but not in OSUSR_1SV_STAGING_FTP1
INSERT INTO OSUSR_1SV_STAGING_FTP1
SELECT * FROM OSUSR_1SV_STAGING_FTP
EXCEPT
SELECT * FROM OSUSR_1SV_STAGING_FTP1
First off you need to have a unique id for each row. Then you can select from Table 1 (OSUSR_1SV_STAGING_FTP) and insert into Table 2 (#OSUSR_1SV_STAGING_FTP1) where it doesn't already exist.
INSERT INTO #OSUSR_1SV_STAGING_FTP1
SELECT *
FROM OSUSR_1SV_STAGING_FTP
WHERE OSUSR_1SV_STAGING_FTP.[Unique_ID] NOT IN
(SELECT Unique_ID FROM #OSUSR_1SV_STAGING_FTP1)
Noel
Related
I have two tables, Table1 contains a list of records with columns super_id, user_name and job_type
Table2 contains a 3 columns as well super_id, view and time
Using a select query with criteria on table I would like to create one record per super_id in Table2
Meaning if the select query was SELECT super_id FROM Table1 WHERE job_type = “Instructor”
RF34323 through RF34328 would appear would each be inserted once into Table2 where the View column is always View1 and time is the current date.
How can an Select Insert query like this written?
The following is an example of the 2 tables:
Is this what you want?
insert into table2 (super_id, `view`, `time`)
select super_id, 'view1', now()
from table1
where job_type = 'Instructor';
Note that view and time are very BAD choices for column names, because they are keywords in SQL.
If you want to create table2, then use create table table2 as instead of insert.
Also, you can default the time column to the insertion time, if you set up the table properly.
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 have an empty "new_table" and "old_1" + "old_2" tables.
When I insert 3000 rows from old_1 to new_table everything is fine.
But when I insert data from old_2 table, auto_increment id's starts from ~4000, not from 3000.. Any ideas why this is happening? I would like to have the new rows starting from 3001.
INSERT INTO new_table ( col1,col2,col3 ) SELECT col1,col2,col3 FROM old_1;
INSERT INTO new_table ( col1,col2,col3 ) SELECT col1,col2,col3 FROM old_2;
EDIT: I havent deleted any rows or messed with auto_increment values at all. I just ran 2 queries in a row
I am trying to populate a products table on MySQL with latest products, which are retrieved and stored in products_temp table.
So the method for this is straight forward, simply doing an INSERT to products from products_temp, as such:
INSERT INTO products ( select products_temp.* FROM products_temp )
Problem is, it results in a duplicate primary key error, because of the id from products_temp clashing with the id in products.
Can someone tell me how to fix this please?
I tried declaring the fields in the select statement without the id, but that results in "Column count doesn't match value count at row 1"
Any help would be appreciated.
Thanks!
You'll need to declare the columns except the ID on both the INSERT and the SELECT, since the number of fields need to match, and id (as you noticed) can't be inserted as is into the destination table.
INSERT INTO DestTable (field1, field2, field3)
SELECT field1, field2, field3 FROM SourceTable;
An SQLfiddle to test with.
EDIT: You could do it in a bit more hacky way to simplify the insert. You can create a trigger that simply forces the primary key to NULL on insert.
CREATE TRIGGER t_DT BEFORE INSERT ON DestTable
FOR EACH ROW
SET NEW.id = NULL;
then a copy from table to table can be done as simply;
INSERT INTO DestTable SELECT * FROM SourceTable;
Another SQLfiddle.
How about something like:
INSERT INTO products
(
select products_temp.* FROM products_temp
where key not in (select key from products)
)
I have a MySql table which has about 100k rows. there is one field say id which contains numbers from 1-35. all these records fall in this range of id i.e. all these records have value of id column between 1-35.
Now i want to create another table which will have one row of each id. i.e the new table should have 35 rows only.
How to go about it ?
create table new_table (id int);
insert into new_table
select distinct id from big_table;
Edit:
You can create the new_table by outputting the big_table create script and changing the name.
SHOW CREATE TABLE big_table;
/* modify the name of the output and execute */
insert into new_table
select * from big_table group by id
You have a table with 100.000 rows, and you want a new table with 35 rows. What values do you want for the remaining columns?
If the answer is: doesn't matter, this works:
CREATE TABLE newTable
SELECT * FROM yourTable
GROUP BY ID;
If you only want the IDs,
CREATE TABLE newTable
SELECT DISTINCT ID FROM yourTable;
You can copy data from one table to another even difference database(Schema) as following
INSERT INTO [DestDatabase].[DestTablName]
SELECT [ColumnName] FROM [SourceDatabase].[SourceTablName];
So, you can use two way:
1:
INSERT INTO tbl_New
SELECT DISTINCT id from tbl_Original;
2:
INSERT INTO tbl_New
SELECT id from tbl_Original GROUP BY id;