Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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_Database_Search - Fatal编程技术网

具有最高组合相邻值的MySQL搜索

具有最高组合相邻值的MySQL搜索,mysql,database,search,Mysql,Database,Search,所以我有一个巨大的数据库表,上面是一个简单的例子。有没有一种方法可以让MySQL执行一个查询,它将输出具有5个最高相邻值的值?我需要将查询调整为5、10、20等 因此,如果将ktim列中每5个相邻单元格相加,最高的5个单元格将为7001.0-7003.5,因为总ktim为1997.53,并将输出以下内容: depth ktim 6999.0 576.46 6999.5 892.10 7000.0 10.12 7000.5 22.07 7001.0 186.55 7001.5

所以我有一个巨大的数据库表,上面是一个简单的例子。有没有一种方法可以让MySQL执行一个查询,它将输出具有5个最高相邻值的值?我需要将查询调整为5、10、20等

因此,如果将ktim列中每5个相邻单元格相加,最高的5个单元格将为7001.0-7003.5,因为总ktim为1997.53,并将输出以下内容:

depth   ktim
6999.0  576.46
6999.5  892.10
7000.0   10.12
7000.5   22.07
7001.0  186.55
7001.5  577.98
7002.0  799.42
7002.5  334.15
7003.0   99.43
7003.5  111.43
7004.0   41.94

表格按深度排序,邻接度将始终根据深度计算。

真正的问题是获得5的最大和以及发生的位置。我认为深度按常规顺序增加了0.5。如果您可以依赖于此,则以下查询将把第一个ktim值相加到当前值,然后返回最大值:

depth   ktim
7001.0  186.55
7001.5  577.98
7002.0  799.42
7002.5  334.15
7003.0   99.43
这将给出最后一行(
7003.0
)。如果需要全部五个,只需将其作为int连接回原始表:

select t.*
from (select t.*,
             (select SUM(ktim) from t t2 where t2.depth between t.depth - 2.25 and t.depth
             ) as lastfivesum
      from t
     ) t
order by lastfivesum
limit 1

我使用
2.25
而不是
2
只是为了防止浮点数出现问题。

是否“相邻”总是意味着“相差0.5”?是的,深度差总是增加0.5。谢谢,这很完美我有两个问题要问。1) SQL输出一个包含两列相同行的表。当我输出代码时,会输出第二组列,因此所有值都是相同的,如何获得相差0.5的第一组列。2) 数据库有几千行,但是如果我的值太高(例如25行),我只得到1个输出。
select t.*
from t join
     (select t.*
      from (select t.*,
                   (select SUM(ktim) from t t2 where t2.depth between t.depth - 2.25 and t.depth
                   ) as lastfivesum
            from t
           ) t
      order by lastfivesum
      limit 1
     ) tmax
     on t.depth bewteen tmax.depth - 2.25 and tmax.depth