How to merge two columns with incomplete information? I have the following schema in my MySQL database, where I have read access:

table1 (customersid*:int, zip1:int) 

table2 (customersfid*:int, zip2:int)


Where customersid references some customers and zip1 and zip2 corresponds to zip code area of that particular customers . Ideally the tables have similar information. However, in my tables zip code is sometimes incomplete:

customersfid is the foreign key of customersid:

Minimal Example:

       table1                      table2
customersid  zip1          customersfid      zip2
1            1001                 1          1001
2            700                  2          700
3            20                   3          20
4            60                   4          60
5            0                    5          30
6            NULL                 6          10
7            33                   7          NULL
8            25                   8          0
9            55                   9          66
10            0                   10         0


I would like to merge them into one column with the following idea (always in favor to zip1):

customersid  zip1                          customersfid  zip2
1            1001                           1           1001
2            700                            2           700
3            20                             3           20
4            60                             4           60
5            30           &lt;-30              5           30
6            10           &lt;-10              6           10
7            33                             7           NULL
8            25                             8           0
9            55         always keep zip1    9           66
10            0                             10          0
You can merge them in a SELECT, no problem. Something like:

SELECT CASE WHEN t1.zip1 in (NULL, 0) then t2.zip2 ELSE t1.zip1 END merged_zip
FROM table1 t1
  INNER JOIN table2 t2 on t1.customersid = t2.customersfid