MySQL: Insert or update if row is newer - mysql

I have table A and B, identical, with a timestamp column. I need to update A with B, adding all rows from B that don't exist in A, and updating any rows that already exist with the same pk. That's trivial to do with the INSERT ... ON DUPLICATE KEY UPDATE clause. However, A might have rows that are actually newer than the same row in B, therefore in those cases where A.last_modified > B.last_modified I don't want to do anything.
Is there any simple solution to that?

You can use left join to get the rows you want
insert into B
select a.*
from A a
left join B b on a.id = b.id
where b.last_modified < a.last_modified or b.last_modified is null
on duplicate key update ...;

Related

Is it possible to update a primary key column?

I was asked to update a SQL Server table's primary key column, the column already has values. On inner joining with few other tables, I had to update the PK column values from another table. It's failing to do so due to duplicates, inserting value 435 to 2 or more PK column.
Any suggestion how it can be done?
UPDATE t
SET t.ID = c.NewID
FROM Table1 t
INNER JOIN Table2 p ON p.ID = t.ID
LEFT OUTER JOIN Table3 c ON c.ID = t.ID
WHERE c.status = 'Y'
Let's say your PK is called A, so:
create a new column called B
update table set B = A
do the changes you need to B
go through all the tables you have FK to your table; drop them, and make sure to reflect the data changes;
drop the PK on A
recreate it on B
recreate all the FKs you dropped 3 steps ago on B
Simple :)
I'm joking, as you can see it is very complicated and error prone. You shouldn't have to worry about your PKs values - if you are there is something wrong in your design

MYSQL Maria DB - Compare 2 tables A and B and update records

I have 2 tables A and B. I wanted to compare table A with table B and for any mismatched record I wanted to update the entire record from table A to Table B. We have 2 primary keys and have around 40 million records.
Can we achieve this in one SQL or Script? Or can I create temporary table(Table c) where I can write table A records which are mismatching and then update Table B with temporary Table C. This I would be doing in MY SQL workbench and using mariaDB DB.
Added additional information - I have 2 primary keys and 15 columns. So, Ideally have to match 13 columns and find mismatches assuming the primary key matches in both tables.
Please assist and appreciate for any feedback.
One approach is to do an update left join of the B table to the A table, and update all columns in the former table with values from the latter. Note that the WHERE clause checks to make sure that a record in B did not match to any record in A.
UPDATE TABLE_B b
LEFT JOIN TABLE_A a
ON a.col1 = b.col1 AND
a.col2 = b.col2 -- AND all other columns
SET b.col1 = a.col1,
b.col2 = a.col2 -- AND set all other columns
WHERE a.col1 IS NULL

Find duplicates on multiple columns in a SQL table in order to create UNIQUE index

I created a index called abc on a table called table with a PRIMARY KEY called id and three others INT columns a, b and c that can be NULL.
Now I need this index to be UNIQUE, so I tried :
ALTER TABLE table DROP INDEX abc, ADD UNIQUE abc (a, b, c);
But I have duplicates, so MySQL answers :
#1062 - Duplicate entry '1-2-3' for key 'abc'
I probably have a lot of those duplicates, so I'm looking for an easy way to search & destroy them all. My first guess has been :
SELECT * FROM table GROUP BY abc
But sadly it seems that you can't group by indexes.
Is there an easy way to find those duplicates, keep one line of each duplicate and delete the others ?
EDIT :
table as an id field that is a PRIMARY KEY
a, b and c are all INT and can be NULL
No need to eliminate duplicates first, just use the IGNORE option for ALTER TABLE to do exactly what you want;
ALTER IGNORE TABLE table DROP INDEX abc, ADD UNIQUE abc (a, b, c);
An SQLfiddle to test with.
If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur. If IGNORE is specified, only one row is used of rows with duplicates on a unique key. The other conflicting rows are deleted.
...and always remember to back up your data before running potentially destructive SQL from random people on the Internet.
SELECT a,b,c
FROM table
GROUP BY a,b,c
HAVING count(*)>1
try this to find dupes
Select a, b, c
From table
group By a, b, c
Having Count(*) > 1
If there is already a unique key column (say its pkColumn) on this table,
you can do this to delete extra dupes.
Delete table
From table t
Where pkColumn <>
(Select Min(pkColumn)
From table
where a = t.a
and b = t.b
and c = t.c)
I'm guessing you have several records that aren't in this situation.
To avoid losing data
CREATE table duplicates SELECT MIN(id) as id, a, b, c, COUNT(1) as nduplicates
FROM yourtable
GROUP BY a,b,c
HAVING COUNT(1)>1;
UPDATE yourtable t, duplicates d
SET t.a='toDelete(or some value that you can easy identify from the rest)'
WHERE d.a=t.a and d.b=t.b and d.c=t.c
and d.id!=t.id;
DELETE FROM yourtable WHERE a='toDelete';
and then drop duplicates table.

Join table on earlier joined table

Say I have got three tables: A, B and C.
A has primary key a
B has primary key a and also a non-primary key c.
C has primary key c.
I want to begin selecting from table A.
So I got a query like this:
Select * from A join B on A.a=B.a join C on B.c=C.c
It returns
Unknown column 'B.c' in 'on clause''
Is this impossible in mysql, joining a table on a joined table? Or am I just doing something wrong? BTW table and column names a made up.
It is possible, and your syntax should be correct.
please refer to this link for syntax:
http://dev.mysql.com/doc/refman/5.0/en/join.html
example to interesting things you can do in MySQL (from the documentation):
SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)
Also note the error claims that column C does not exist - so you should check the structure of B..

Using MySQL, I'd like to update 1 tables id values from another and use ma

I'm trying to write a query to update a FK column in table B using the primary key column in table A. If there are duplicate entries in table A, I'd like to use the max id of the duplicate entry to insert into table B.
I have the first part of the query written but I'm unsure about the duplicate entry part.
Here's what I have so far...
UPDATE calliope_media.videos v
JOIN calliope_media.video_ingress_queue viq ON v.provider_unique_id = viq.provider_unique_id
SET v.video_ingress_id = viq.id;
This is how your query should look.
UPDATE B
SET B.the_column_ID = (SELECT MAX(A.some_ID)
FROM A
WHERE A.matching_value = B.matching_value)
This is the overall structure. I haven't adapted to your specific requirements, since I don't fully understand them. But this should get you back on track.