'Combining 2 Tables, Want to keep all Non 0 Bit on DUPLICATE KEY
I have a table with 1000 columns (yes its normalized), that is storing Biiig Daaata!
I need to Insert Update new data as it becomes available overnight, and figured the fastest way to do that is to take the data, generate a CSV file and insert into table 2.
Table 1 Table2
(Real Data) (Data to be merged)
Then do something like:
INSERT INTO blueData b
SELECT *
FROM rawData r
ON DUPLICATE KEY
UPDATE _____
This is where I draw a blank, basically all the fields are bit fields (except PK) that is 1 or 0. I want to keep all the fields with 1 in both tables, the rest can stay zero.
Re-Explained, the Table2 will have new data that relates to table 1. I need to set the new bit fields from table 2 to the corresponding PK on table 1 without removing its 1 bit fields. But because it generates a CSV the 0's are being generated in the columns that arnt new for the data.
Table 1:
Row1:
PK: abc
BI: 1
BG: 0
BD: 0
BF: 0
Table 2:
Row1:
PK: abc
BI: 0
BG: 1
BD: 1
BF: 0
Needs to end up like: Table1
Row1:
PK: abc
BI: 1
BG: 1
BD: 1
BF: 0
Hopefully this all makes sense :D
I need help writing the update part on duplicate key, only deal is with 1000 columns be nice if I didn't have to write a giant SQL statement that does if's on every column (will generate one in Python if I have to) but would make query huge!
Solution 1:[1]
Try use logical OR
INSERT INTO Table1
SELECT * FROM Table2 b
ON DUPLICATE KEY UPDATE
Table1.BI = (Table1.BI OR b.BI),
Table1.BG = (Table1.BG OR b.BG),
Table1.BD = (Table1.BD OR b.BD),
Table1.BF = (Table1.BF OR b.BF)
It will give you this in Table1
| PK | BI | BG | BD | BF |
---------------------------
| abc | 1 | 1 | 1 | 0 |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |