I have a table that I wish to update (or insert if no previous condition exists)
servers table clusters table datacenters table
server_id | server_name cluster_id | cluster_name datacenter_id | datacenter_name
----------------------- ------------------------- -------------------------------
1 server1 1 cluster1 1 datacentereast
2 server7 3 Cluster22 5 datacenterwest
server_status table
status_id | status_server | status_cluster | status_datacenter
---------------------------------------------------------------------
I wish to populate server_status with the ids of the other tables and avoid duplicates.
So far I have :
sql_string = """
INSERT into server_status (status_datacenter,status_cluster, status_server)
SELECT
d.datacenter_id,c.cluster_id,s.server_id
FROM
datacenters as d, clusters as c, servers as s
WHERE
d.datacenter_name = \'%s\'
AND c.cluster_name = \'%s\'
AND s.server_name = \'%s\'
AND d.datacenter_id != status_datacenter
AND c.cluster_id != status_cluster
AND s.server_id != status_server;
""" % (datacenter,cluster,server)
The above fails with:
_mysql_exceptions.OperationalError: (1054, "Unknown column 'status_datacenter' in 'where clause'")
Print out of an actual sql command:
INSERT into server_status (status_datacenter,status_cluster, status_server)
SELECT d.datacenter_id,c.cluster_id,s.server_id
FROM datacenters as d, clusters as c, servers as s
WHERE d.datacenter_name = 'SDDC'
AND c.cluster_name = 'qboc37'
AND s.server_name = 'ap30'
AND d.datacenter_id != status_datacenter
AND c.cluster_id != status_cluster
AND s.server_id != status_server;
First create a unique index on
server_status (status_datacenter,status_cluster, status_server), then