简体   繁体   中英

Merging similar tables in SQL part 2

I think people were reluctant to help me last time because I didn't want to post up the columns, but I'm eager to solve this, so I'm putting aside my better judgement and laying it bare.

OK, I have two similar tables, and I'm using MySQL, and PHP to generate the tables. So far I have 3 tables:

CPE:
fastethernet00,subnet00,fastethernet01,subnet01,hsrp,vip,bgp,bgp_neighbor,remote_as,bgp_customer_net,next_hop,loopback,cpe,hostname,cpe_cust_index,int_next_hop_1,int_next_hop_2,voice,solution

Liverpool:
apn1,apn2,apn3,apn4,apn5,customer,vpi_vci,vlan1,cust_index,RADIUS,nexthop,atinterface1,atinterface2,feinterface1,feinterface2,spinterface1,spinterface2,ip_address_range1,ip_address_range2,handset_address_range1,handset_address_range2,handset_address_range3,handset_address_range4,handset_address_range5,handset_address_range6,handset_address_range7,handset_address_range8,handset_address_range9,handset_address_range10,handset_address_range11,handset_address_range12,handset_address_range13,handset_address_range14,handset_address_range15,handset_address_range16,DNS_Server1,DNS_Server2,OLVDMVPN,live

Greenwich:
apn_1,apn_2,apn_3,apn_4,apn_5,customer_,vpi_vci_,vlan_1,cust_index_,RADIUS_,nexthop_,atinterface_1,atinterface_2,feinterface_1,feinterface_2,spinterface_1,spinterface_2,ip_address_range_1,ip_address_range_2,handset_address_range_1,handset_address_range_2,handset_address_range_3,handset_address_range_4,handset_address_range_5,handset_address_range_6,handset_address_range_7,handset_address_range_8,handset_address_range_9,handset_address_range_10,handset_address_range_11,handset_address_range_12,handset_address_range_13,handset_address_range_14,handset_address_range_15,handset_address_range_16,DNS_Server_1,DNS_Server_2,OLVDMVPN_,live_

The first step I need to accomplish is linking Greenwich to Liverpool via apn_1/apn1. The same record won't necessarily be on both, and there may even be duplicates. The php I'm using to create this table is throwing up a syntax error around the UNION ALL:

mysql_query ("CREATE TABLE Both (
SELECT * FROM `Greenwich` left outer join Liverpool on Greenwich.apn_1 = Liverpool.apn1
UNION ALL
SELECT * FROM Greenwich
RIGHT OUTER JOIN Liverpool on Greenwich.apn_1 = Liverpool.apn1  where Greenwich.apn_1 is null)")

Can anyone see what's wrong with that?

Afterwards, I need to then link the resultant table to cpe_cust_index via cust_index/cust_index_, so ideally I think I need to merge these cells. If these cells are present in Greenwich and Liverpool they will always be the same.

Thanks in advance!

The answer is the 3rd way described here, using a "mutex (mutually exclusive) table

http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/

This plus a filter give me exactly what I need. Now I just have to merge the index fields together, and apn 1 fields together.

Try naming all the columns that you want to select and alias then using AS so that they match. Union does not like column name mismatches.

Eg.

Select col1 as col1, col2 as col2 from table1
Union
Select col-1 as col1, col-2 as col2 from table2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM