Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从mysql结果中获取最新记录_Mysql_Sql - Fatal编程技术网

从mysql结果中获取最新记录

从mysql结果中获取最新记录,mysql,sql,Mysql,Sql,我有我的mysql查询,我在php脚本中使用它: select distinct dhcp_logs_public.service, dhcp_logs_public.ip, dhcp_logs_public.date as date, dhcp_logs_public.until as until from dhcp_logs_public where dhcp_logs_public.ip in('79.109.1.200','71.109.160.123','21.110.151.110'

我有我的mysql查询,我在php脚本中使用它:

select distinct dhcp_logs_public.service,
dhcp_logs_public.ip,
dhcp_logs_public.date as date,
dhcp_logs_public.until as until
from dhcp_logs_public
where dhcp_logs_public.ip in('79.109.1.200','71.109.160.123','21.110.151.110')
AND dhcp_logs_public.date >= DATE_ADD(NOW(), INTERVAL -48 Hour)
这给了我这样的结果:

service         ip                date            until
PONS1   79.109.1.200    11.10.2014 17:17    11.10.2014 19:17
PONS2   71.109.160.123  11.10.2014 19:09    12.10.2014 3:09
PONS2   71.109.160.123  11.10.2014 23:09    12.10.2014 7:09
PONS2   71.109.160.123  12.10.2014 3:09     12.10.2014 11:09
PONS3   71.109.160.123  12.10.2014 7:09     12.10.2014 15:09
PONS4   71.109.160.123  12.10.2014 16:19    12.10.2014 17:19
PONS4   71.109.160.123  12.10.2014 16:49    13.10.2014 0:49
PONS5   21.110.151.110  13.10.2014 9:22     13.10.2014 11:22
PONS5   21.110.151.110  13.10.2014 10:34    13.10.2014 12:34
PONS5   21.110.151.110  13.10.2014 11:46    13.10.2014 13:46
PONS5   21.110.151.110  13.10.2014 11:46    13.10.2014 13:46
我需要修改我的查询以实现这一点:(每个ip地址的最新记录)

我尝试过子查询之类的方法,但我的表太大(约10亿行),无法快速处理它。此外,我还试图添加最大(日期)组的ip,但没有运气


有什么想法吗?

你确定分组方式不起作用吗

否则,请尝试以下方法:

SELECT DISTINCT MAX(dhcp_logs_public.service) as service, dhcp_logs_public.ip, MAX(dhcp_logs_public.date) as date, MAX(dhcp_logs_public.until) as until FROM dhcp_logs_public WHERE dhcp_logs_public.ip in('79.109.1.200','71.109.160.123','21.110.151.110') AND dhcp_logs_public.date >= DATE_ADD(NOW(), INTERVAL -96 Hour) GROUP BY dhcp_logs_public.ip 选择不同的 MAX(dhcp_日志_public.service)作为服务, dhcp_日志_public.ip, 最大值(dhcp_日志_public.date)作为日期, 最大值(dhcp_日志_public.until)为until 从dhcp_日志_公共 其中,dhcp_将_public.ip登录到('79.109.1.200'、'71.109.160.123'、'21.110.151.110') 和dhcp_logs_public.date>=date_ADD(现在(),间隔-96小时) 按dhcp分组\u日志\u public.ip
您可以使用
not exists
选择不存在具有相同ip和较新日期的另一行的所有行(这意味着所选行是其ip地址的最新行)

此查询可以利用
(ip,日期)

编辑

如果您可以依靠
id
列来确定行的最近时间,那么下面的操作可能会更快

select d.service,
d.ip,
d.date,
d.until
from dhcp_logs_public d
where d.ip in in('79.109.1.200','71.109.160.123','21.110.151.110')
and d.date >= DATE_ADD(NOW(), INTERVAL -48 Hour)
and d.id = (select max(id) from dhcp_logs_public d2 where d2.ip = d.ip)
或者使用派生表而不是子查询

select d.service,
d.ip,
d.date,
d.until
from dhcp_logs_public d
join (
    select max(id) max_id
    from dhcp_logs_public
    where d.ip in in('79.109.1.200','71.109.160.123','21.110.151.110')
    and d.date >= DATE_ADD(NOW(), INTERVAL -48 Hour)
    group by ip 
) t1 on t1.max_id = d.id

这将不起作用,因为您只按IP分组,但希望显示所有字段当您按字段分组时,您保留其他字段,我在保存每个IP最后一条记录的所有字段的请求中按排序。否?否,因为当您仅按ip分组时,数据库系统谁应该知道它应该显示哪个服务编号、日期或直到字段?是^^^有了这个,它将为每个ipno获取最后一条记录,我之前已经测试过这个。这种查询得到3个结果,但不是最新的结果。日期在表示层重新格式化?我不确定我是否理解正确,但日期列没有重新格式化。数据是直接形式的DB。SQL中的日期遵循特定的格式。你不能偏离它。我有日期和截止列的时间戳格式。所以上面显示的结果片段不是“直接来自数据库”!?!?事实上,这是有效的,唯一的问题是性能。。使用3个ip进行的查询运行了约8秒。。。我的ip和日期列上有索引。@Viktors你有两个单独的索引还是一个复合索引?
select d.service,
d.ip,
d.date,
d.until
from dhcp_logs_public d
where d.ip in in('79.109.1.200','71.109.160.123','21.110.151.110')
and d.date >= DATE_ADD(NOW(), INTERVAL -48 Hour)
and d.id = (select max(id) from dhcp_logs_public d2 where d2.ip = d.ip)
select d.service,
d.ip,
d.date,
d.until
from dhcp_logs_public d
join (
    select max(id) max_id
    from dhcp_logs_public
    where d.ip in in('79.109.1.200','71.109.160.123','21.110.151.110')
    and d.date >= DATE_ADD(NOW(), INTERVAL -48 Hour)
    group by ip 
) t1 on t1.max_id = d.id