I would like to merge 3 tables data, each have a number column in common,
but all the other data differs.
What I would like to achieve is selecting all of the data from the common column
and merging the rest of the columns from all the tables based on the number column.
Please keep in mind that the number column can have duplicates between the tables.
t1
------------------------------
| number | stuff 1 | stuff 2 |
------------------------------
| 1 | x | y |
------------------------------
t2
-------------------------------------------------------
| number | data 1 | data 2 | data 3 | data 4 | data 5 |
-------------------------------------------------------
| 2 | a | b | c | d | e |
-------------------------------------------------------
t3
----------------------------------------------
| number | info 1 | info 2 | info 3 | info 4 |
----------------------------------------------
| 1 | j | k | l | m |
----------------------------------------------
| 3 | j | k | l | m |
----------------------------------------------
Merged Table
------------------------------------------------------------------
| number | stuff 1 | stuff 2 | data 2 | data 5 | info 1 | info 4 |
------------------------------------------------------------------
| 1 | x | y | | | l | m |
------------------------------------------------------------------
| 2 | | | b | e | | |
------------------------------------------------------------------
| 3 | | | | | l | m |
------------------------------------------------------------------
There must be no duplicates of the actual number, but any data across the 3 tables for that number must be merged into one row.
What would be a bonus is if it could do ON DUPLICATE KEY UPDATE
, So I can run this query once every few days to add any new data
Thank you in advance
Best Answer
This query assumes the
number
field is primary or at least unique key in each table.UPDATE:
If You got error You describe that is NOT MySQL error.
http://www.sqlfiddle.com/#!9/22d82dc/1