Maria update from union - or suggestion

34 Views Asked by At

I have a Maria DB and I'm trying to update a table from two other tables. Archive Table - timestamped data up to 00 UTC last night. Current - timestamped data from 00 to now. I want to copy the results from both tables to a column in a summary table.

Archive Table 
time|result
1   A
2   B
3   C
4   D
5   E

Current table:
time|result
6   1
7   2
8   3
9   4
10  5

Summary Table:
time|result
1   A   
2   B
3   C
4   D
5   E
6   1
7   2
8   3
9   4
10  5

I've been able to update based on a single INNER JOIN between the summary table and either the archive or Current table independently, but I haven't been able to figure out how to combine it.

Since Archive and Current are identical and don't overlap, it seems like I should UNION the archive and current and then update the summary table? I've also tried performing two LEFT OUTER JOINS and filter out the NULLS, but I'm still missing something.

Is this something I should be using a sub query on?

I'm completely new to this and only need to get past a couple of queries for a project. I'm hoping someone can help point me in the right direction.

Thanks for any advice

1

There are 1 best solutions below

0
On

I'm not sure from your question whether your Summary table is empty or not.

Assuming it's empty and you need to populate it, then UNION ALL will do, which doesn't strip duplicates and saves time (as opposed to UNION which under the hood sorts records and then removes duplicates):

insert into summary_table (time, result)
select time, result from archive_table
union all
select time, result from current_table