简体   繁体   English

MySQL嵌套查询选择语句

[英]MySQL Nested Query Select Statement

I've got a table that helps me keep track of the delay times between my slaves and the master. 我有一张桌子,可以帮助我跟踪从机和主机之间的延迟时间。 My question is how can I craft a select statement that: 我的问题是我该如何选择以下语句:
1. gives me the latest delay values, without repeating (or skipping) ip addresses 1.给我最新的延迟值,而无需重复(或跳过)ip地址
2. doesn't need to be updated if I add additional servers, or as servers become unresponsive 2.如果添加其他服务器,或者服务器无响应,则不需要更新

The goal of this query is to show me what servers are available to do work, and give me a rough estimate as to how hard they are working. 该查询的目的是向我展示可以使用哪些服务器来工作,并大致估计它们的工作强度。 Servers that are not operational, shouldn't appear in the results. 无法运行的服务器不应出现在结果中。 I'm running a script to evaluate the delay times as CLI every minute, so if I could limit the possible records returned to the last minute and a half, that should be good enough to tell me which servers were up the last time they were queried. 我正在运行一个脚本,以每分钟为单位评估CLI的延迟时间,因此,如果我可以将可能返回的记录限制到最后一分钟半,那应该足以告诉我上次服务器已启动了哪些服务器。查询。

Table looks like this (columns renamed to protect the innocent): 表格如下所示(将列重命名为保护无辜者):

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,  
ip VARCHAR( 20 ) NOT NULL ,  
sent TIMESTAMP NOT NULL ,  
delay DECIMAL ( 7,4 ) NOT NULL ,  
status VARCHAR( 100 ) NOT NULL ,  
execution_time` DECIMAL ( 7,4 )NOT NULL ,  
deleted` TINYINT NOT NULL ,  

Any help would be appreciated. 任何帮助,将不胜感激。

What about this? 那这个呢?

select delay 
    from table_name, 
         (select id, max(sent), ip from table_name group by ip) innertable 
   where innertable.id = table_name.id;

Not 100% sure I understood what you want as a result set, so I just picked delay. 不能100%地确定我了解您想要的结果集,所以我只是选择了延迟。

After some googling, and some testing, this is the best answer I've found thus so far: 经过一番谷歌搜索和测试之后,这是到目前为止我发现的最佳答案:

    SELECT ip, id, delay, stat_sent
    FROM status
    WHERE stat_sent > DATE_SUB( NOW( ) , INTERVAL 1 MINUTE )
    AND stat_sent
    IN (

        SELECT max( stat_sent )
        FROM status GROUP BY stat_ip
    )

Modified my answer now it limits the scope of the results to servers that have been updated in the last minute. 现在修改了我的答案,将结果范围限制为最近一分钟更新的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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