mysql语句嵌套查询语句_MySQL嵌套查询选择语句

bd96500e110b49cbb3cd949968f18be7.png

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

2. doesn't need to be updated if I add additional servers, or as servers become unresponsive

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.

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值