/*Insert from tblContacts*/
INSERT INTO addresses(
address_type,
owner_type,
owner_id,
address1,
address2,
city,
state,
zip_code,
created_date,
created_by,
modified_date,
modified_by)

SELECT * FROM(
/*for the Mailing type of the record */
SELECT
"M",
"P",
`ID`,
con_pobox, /*put the address value that is currently in pobox into the 1st address field*/
NULL con_address,
con_city,
con_state,
con_zip,
NOW() created_date,
1271 created_by,
NOW() modified_date,
1271 modified_by

FROM spcsta5_floorplans.tblContacts
WHERE con_pobox LIKE 'PO Box%' AND con_address is not null AND con_address NOT LIKE 'PO Box%' #52
    
UNION 

/*for the Legal type of the same record */
SELECT
"L",
"P",
`ID`,
con_address,
NULL con_pobox, 
con_city,
con_state,
con_zip,
NOW() created_date,
1271 created_by,
NOW() modified_date,
1271 modified_by

FROM spcsta5_floorplans.tblContacts
WHERE con_pobox LIKE 'PO Box%' AND con_address is not null AND con_address NOT LIKE 'PO Box%' #Also 52

UNION
SELECT
"B",
"P",
`ID`,
con_address,
NULL con_pobox,
con_city,
con_state,
con_zip,
NOW() created_date,
1271 created_by,
NOW() modified_date,
1271 modified_by

FROM spcsta5_floorplans.tblContacts
WHERE con_address LIKE 'PO Box%' AND con_pobox is null #49

UNION

/*This for M and L*/
SELECT
"B",
"P",
`ID`,
con_address,
con_pobox,
con_city,
con_state,
con_zip,
NOW() created_date,
1271 created_by,
NOW() modified_date,
1271 modified_by

FROM spcsta5_floorplans.tblContacts

WHERE con_address NOT LIKE 'PO Box%' AND con_pobox NOT LIKE 'PO Box%'
AND con_address is not null AND con_pobox is not null #49


UNION

SELECT
"B",
"P",
`ID`,
con_address,
NULL con_pobox,
con_city,
con_state,
con_zip,
NOW() created_date,
1271 created_by,
NOW() modified_date,
1271 modified_by

FROM spcsta5_floorplans.tblContacts

WHERE con_address NOT LIKE 'PO Box%'
AND con_address is not null AND con_pobox is null #490

UNION
    
SELECT
"B",
"P",
`ID`,
con_pobox, /*put the address value that is currently in pobox into the 1st address field*/
NULL con_address,
con_city,
con_state,
con_zip,
NOW() created_date,
1271 created_by,
NOW() modified_date,
1271 modified_by

FROM spcsta5_floorplans.tblContacts

WHERE con_pobox LIKE 'PO Box%'
AND con_address is null #5

UNION

SELECT
"M",
"P",
`ID`,
con_address,
con_pobox,
con_city,
con_state,
con_zip,
NOW() created_date,
1271 created_by,
NOW() modified_date,
1271 modified_by

FROM spcsta5_floorplans.tblContacts

WHERE con_address LIKE 'PO Box%'
AND con_pobox is not null AND con_pobox NOT LIKE 'PO Box%' #0

UNION

/*for the Legal type of these records */
SELECT
"L",
"P",
`ID`,
con_pobox, /*put the address value that is currently in pobox into the 1st address field*/
NULL con_address,
con_city,
con_state,
con_zip,
NOW() created_date,
1271 created_by,
NOW() modified_date,
1271 modified_by

FROM spcsta5_floorplans.tblContacts

WHERE con_address LIKE 'PO Box%'
AND con_pobox is not null AND con_pobox NOT LIKE 'PO Box%' #0

UNION

/* Same query for the Mailing type of these records */
SELECT
"M",
"P",
`ID`,
con_address,
NULL con_pobox,
con_city,
con_state,
con_zip,
NOW() created_date,
1271 created_by,
NOW() modified_date,
1271 modified_by

FROM spcsta5_floorplans.tblContacts

WHERE con_address LIKE 'PO Box%'
AND con_pobox is not null AND con_pobox NOT LIKE 'PO Box%' #0


UNION
SELECT
"B",
"P",
`ID`,
con_pobox, /*put the address value that is currently in pobox into the 1st address field*/
NULL con_address,
con_city,
con_state,
con_zip,
NOW() created_date,
1271 created_by,
NOW() modified_date,
1271 modified_by

FROM spcsta5_floorplans.tblContacts

WHERE con_address is null
AND con_pobox is not null AND con_pobox NOT LIKE 'PO Box%') consolid; #0