我试图想出一个SQL语句来打印Puppet数据库中的所有重复[导出资源]定义。在SELECT语句中用另一个表替换另一个值
mysql> SELECT id,restype,host_id,source_file_id FROM resources
-> WHERE title IN (SELECT title FROM resources WHERE exported=1
-> GROUP BY title HAVING count(title) > 1) ORDER BY title;
+------+------------------------+---------+----------------+
| id | restype | host_id | source_file_id |
+------+------------------------+---------+----------------+
| 305 | Nagios::Client::Export | 2 | 18 |
| 333 | Nagios_host | 2 | 39 |
| 605 | Nagios_hostextinfo | 6 | 2 |
| 443 | Nagios_hostextinfo | 2 | 39 |
| 499 | Nagios_host | 6 | 2 |
| 770 | Nagios::Client::Export | 6 | 18 |
......
......
这是工作得很好,但我怎么能检索/打印hosts.name从主机表中host_id
代替。我只是无法重写上面的SQL语句。该主机表看起来像这样:
mysql> SELECT id,name FROM hosts;
+----+-----------------------------------------+
| id | name |
+----+-----------------------------------------+
| 2 | controller-dns-01.sdas.cloud.com |
| 6 | controller-monitoring-01.sdas.cloud.com |
| 1 | controller-puppet.sdas.cloud.com |
| 13 | talend-admin-01.sdas.cloud.com |
| 15 | talend-jobserver-01.sdas.cloud.com |
| 14 | talend-jobserver-02.sdas.cloud.com |
+----+-----------------------------------------+
此外,有没有办法只打印在整个字符串代替的主机名(即仅controller-dns-01
)的第一部分?任何人的任何建议不胜感激。干杯!!
更新:
这是我最后的命令:就在情况下,如果别人也在寻找一种方式来找出木偶导出的资源重复定义
mysql> CREATE INDEX index_resources_on_restypetitle ON resources (restype(12),title(12));
mysql> SELECT r.id, r.restype, r.title, SUBSTRING_INDEX(h.name,'.',1) AS 'host_name',
-> SUBSTRING_INDEX(s.filename,'puppet/',-1) AS 'file_name', r.line FROM resources r
-> LEFT JOIN hosts h ON r.host_id = h.id LEFT JOIN source_files s ON r.source_file_id = s.id
-> WHERE MD5(CONCAT(restype,title,host_id))
-> IN (SELECT MD5(CONCAT(restype,title,host_id)) FROM resources
-> WHERE exported=1 GROUP BY MD5(CONCAT(restype,title,host_id))
-> HAVING COUNT(MD5(CONCAT(restype,title,host_id))) > 1) ORDER BY title;
的SUBSTRING_INDEX(s.filename....)
位可能需要根据配置重新调整。非常感谢thiella帮助我。
使用'SUBSTRING_INDEX()'函数来得到第一部分主机名。 – Barmar 2013-04-06 10:10:28
@Barmar:谢谢!完全忘了那个SUBSTRING_INDEX()函数。干杯!! – MacUsers 2013-04-06 11:07:40