Mysql 在同一列上左连接多个列

标签 mysql

基本上我想要实现的目标是 test.zfirewallaudit.src_iptest.zfirewallaudit.dst_ip要加入 inventory.dns_zones.ip 的字段列,所以我基本上可以为 inventory.dns_zones.subdomain 提供准确的主机名( test.zfirewallaudit.src_ip )和 test.zfirewallaudit.dst_ip 。所以基本上我想要一个 src_ip 子域的列和一个 dst_ip 子域的列。

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;

问题是,我需要 inventory.dns_zones.subdomain对于test.zfirewallaudit.src_ip...dst_ip值(value)观。当前查询只给了我 1 subdomain栏目

最佳答案

您需要加入 inventory.dns_zones 两次,一次用于源,一次用于目标:

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;

关于Mysql 在同一列上左连接多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45969524/

相关文章:

mysql - 使用 html/jsp 中的文件上传将 blob 图像插入数据库

mysql - Sql 查询使用 SQL 查询将第二行数据显示到第一行,将第三行数据显示到第二行,依此类推

mysql - 提高mysql存储函数的性能

php - 我如何根据解释结果改进此查询

mysql - 通过查询优化MySQL顺序

MySql 字符串替换查询不起作用

mysqldump 大数据库增量

php - Mysql有唯一记录键吗?

mysql - 使表单安全地提交变量

mysql - SQL多对多关系解决方案: intermediate table or xml query?