SQL Join Two Result Sets / Join Two Select Statement Results into one Result Set

Thanks to Mark Byers on Stack Overflow.


SELECT T1.col_a, T1.col_b, T2.col_c
FROM(SELECT col_a, col_b,...etc...)AS T1
JOIN(SELECT col_a, col_c,...etc...)AS T2
ON T1.col_a = T2.col_a

SQL Join Example – if null (ifnull)

A useful SQL join example where the if null (ifnull) function is used.

 

insert into lpt_register_appends_3
(  name,
   add_line1,
   add_line2,
   add_line3,
   add_line4,
   age,
   salary,
   company_id,
   company_address,
   fav_band,
   fav_color,
   fav_team,
   fav_ice_cream,
   shoe_size,
   eye_color,
   hair_color,
   hair_style,
   fav_food,
   tea_coffee,
   description,
   date
)
SELECT
ifnull(eit_one.name,”),
ifnull(eit_one.addr1,”),
ifnull(eit_one.addr2,”),
ifnull(eit_one.addr3,”),
ifnull(eit_one.addr4,”),
ifnull(eit_two.age,null),
ifnull(eit_two.salary,null),
ifnull(eit_two.building_id,null),
ifnull(eit_two.address_point_id,null),
‘NF’,
‘NF_’+ifnull(eit_two.key,’427’),
eit_two.rank,
eit_two.quality,
‘111222333w’,
‘1’,
‘1’,
null,
null,
‘0’,
‘This is the description’,
‘20130305.00000000’
FROM employee_info_table_one eit_one
left join employee_info_table_two eit_two on eit_one.id = eit_two.id