Mysql left join multiple columns on same column

时间:2017-08-30 20:53:25

标签: mysql

Basically what I'm trying to accomplish, is I want the a = Outer() b = Outer() print(a is b) print(a.zz is b.zz) print(a.zz, b.zz) AND zz fields to join on the False True Small(item=0.9892612860711646) Small(item=0.9892612860711646) column, so i can basically have an accurate hostname (test.zfirewallaudit.src_ip) for test.zfirewallaudit.dst_ip AND inventory.dns_zones.ip. so basically I want a column for src_ip subdomain and a column for dst_ip subdomain.

inventory.dns_zones.subdomain

Problem is, I need the test.zfirewallaudit.src_ip for both the test.zfirewallaudit.dst_ip and SELECT test.zfirewallaudit.src_ip, test.zfirewallaudit.src_zone, test.zfirewallaudit.dst_ip, test.zfirewallaudit.dst_zone, test.zfirewallaudit.dst_port, test.zfirewallaudit.rulename, test.zfirewallaudit.application, inventory.dns_zones.subdomain FROM test.zfirewallaudit LEFT JOIN inventory.dns_zones ON zfirewallaudit.src_ip = inventory.dns_zones.ip OR zfirewallaudit.dst_ip = inventory.dns_zones.ip LIMIT 10; values. The current query only gives me 1 inventory.dns_zones.subdomain column

1 个答案:

答案 0 :(得分:1)

您需要加入inventory.dns_zones两次,一次为src,一次为dst:

SELECT test.zfirewallaudit.src_ip, 
test.zfirewallaudit.src_zone, 
test.zfirewallaudit.dst_ip, 
test.zfirewallaudit.dst_zone, 
test.zfirewallaudit.dst_port, 
test.zfirewallaudit.rulename, 
test.zfirewallaudit.application,
dns_zones_src.subdomain,
dns_zones_dst.subdomain
FROM test.zfirewallaudit
LEFT JOIN inventory.dns_zones AS dns_zones_src ON ( zfirewallaudit.src_ip = dns_zones_src.ip )
LEFT JOIN inventory.dns_zones AS dns_zones_dst ON ( zfirewallaudit.dst_ip = dns_zones_dst.ip )
LIMIT 10;