Alternative to REPLACE INTO TABLE mysql query - mysql

I usually copy data into tabelB from table A daily by the following query:
REPLACE INTO tableB (id,col1,col2,col3) SELECT id,col1,col2,col3 FROM tableA WHERE date='somedate'
Here REPLACE is used as if the script is being run double for a particular date by mistake, it will not copy the tableA two times into tableB. But problem is, tableB has two unique key index (id and col1). So if REPLACE is executed more than once, index is deleted and recreated by this REPLACE command. To avoid this thing, I want to use:
INSERT INTO tableB (id,col1,col2,col3) VALUES (val1,val2,val3,val4) ON DUPLICATE KEY UPDATE col2=VALUES(col2),col3=VALUES(col3)
But as I am copying the tableA here, I cant use the above INSERT........ON DUPLICATE KEY UPDATE query, I have to use SELECT command to copy data from tableA.
So how to modify INSERT INTO ........... ON DUPLICATE KEY UPDATE query, so that it can copy the tableA and at the same time if execute the script more than once, it just updates the data.
Regards

Try this INSERT query -
INSERT INTO tableB (id, col1, col2, col3)
SELECT id, col1, col2, col3 FROM tableA WHERE date = 'somedate'
ON DUPLICATE KEY UPDATE col2 = VALUES(col2), col3 = VALUES(col3);

Related

Insert daily table to main table and update row in main table if key exists

I am attempting to insert a daily table to a main table that has primary keys in it. I have the following architecture:
A python scripts that generates data in a pandas dataframe-> drops the daily table -> pushes to the daily table -> pushes the data from the daily table to the main table.
I cannot drop the main table but I can drop the daily table. At the moment I am using the following query to perform this:
REPLACE INTO
mydb.{to_table}
SELECT
*
FROM
mydb.{from_table};
but I am getting the following error message:
ERROR (1062, "Duplicate entry '2147483647' for key 'PRIMARY'") with sql
REPLACE INTO
mydb.book_authors_v2
SELECT
*
FROM
mybd.books_authors_v2_daily;
I've tried using INSERT ... UPDATE ON DUPLICATE KEY but it did not manage to get it to work.
Is there any way of inserting the new rows from the daily table to the main table and updating the existing rows in the main table? There are nearly daily changes to the structure of the table, I would like to avoid having to manually define the columns.
Yes, this looks like a good use case for the the INSERT ... ON DUPLICATE KEY syntax.
Assuming that the primary key of your table is called id, and you have 3 other columns col1, col2, col3 that you want to update on duplicate id, that would look like:
insert into mydb.book_authors_v2(id, col1, col2, col3)
select id, col1, col2, col3
from mybd.books_authors_v2_daily d
on duplicate key update set col1 = d.col1, col2 = d.col2, col3 = d.col3

Inserting data from one table to another without duplication in access-VBA

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

Join 2 tables with same PRIMARY key that is autoincrementing too

I have 2 tables with both of them having the first column as PRIMARY which is also an auto incrementing. First table has 67 entries starting from 1 to 67 and the second table has 48 entries. Both of them have same columns. I want to take the content from Table2 and insert them into Table1 such that the next entry in Table1 starts from 68. Finally I will have 115 entries in Table1 and the PRIMARY column will also show that. I tried this:
INSERT INTO `Table1` SELECT * FROM `Table2`
But it said
#1062 - Duplicate entry '1' for key 'PRIMARY'
What do I do?
Name the columns you want to insert and leave the auto_incrment columns from the insert
INSERT INTO `Table1` (col2, col3, col4)
select col2, col3, col4 from `table2`
You need to specify the columns you wish to enter, without the identity column.
Something like
INSERT INTO `Table1` (column1, column2,...,columnn)
SELECT column1, column2,...,columnn FROM `Table2`
SET IDENTITY_INSERT database_name.schema_name.table ON
--Do Insert Here
SET IDENTITY_INSERT database_name.schema_name.table OFF
Make sure there is no duplicate id thought.
See this page
try out this...
INSERT INTO Table1 (col2, col3,...)
select col2, col3,... from Table2
you can do it with n number of columns...
Here you need to specify column name on that you wish to insert data other than identity column(Auto Increment Column).

MySQL Single Table Scan Update

Is there a way to accomplish a single table scan in MySQL with an UPDATE? The following is a standard example:
IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
ELSE
INSERT INTO Table1 VALUES (...)
This is the ideal situation I'd like to happen in MySQL (But this is MsSQL):
UPDATE user SET (name = 'jesse') WHERE userid ='10001'
IF ##ROWCOUNT=0
INSERT INTO user (name) VALUES('jeeeeee')
It's sort of reversed in MySQL. You perform the insert, and if the key already exists, then update the row:
INSERT INTO Table1 (col1,col2,col3) VALUES (val1,val2,val3)
ON DUPLICATE KEY UPDATE col1 = val1, col2 = val2, col3 = val3;
This is predicated on you having a unique key for the table (which you do, right?)

How to load values of one table to another automatically in MySQL..?

there is a existing table A. suppose i want to add all or specific(column) values of an existing table A to table B using foreign key, how do i do it in MySQL?
and if there is any new insert or update in table A it should automatically insert into table B also..
To automatically update Table B from changes in Table A would require triggers that MySQL supports but phpMyAdmin does not. If instead you're looking to insert rows into Table B from Table A on an ad-hoc basis then that's simple
INSERT INTO TABLEA (COL1, COL2, COL3)
SELECT FROM TABLEB (COL1, COL2, COL3)
WHERE (SELECT COUNT(*) FROM TABLEA WHERE TABLEA.COL1 = TABLEB.COL1) = 0
The above SQL does a simple copy from TableB into TableA. The WHERE clause ensures only records which don't already exist are inserted.
You can use insert/update trigger for that.
Why should you want to do that?
Why don't you just reference the data in table A using the forign key in table B you mentioned?
insert into tableb(columns) select columns from tablea