gpt4 book ai didi

php - 将 2 列中的确切 Tandem 值分组?

转载 作者:行者123 更新时间:2023-11-29 02:30:58 25 4
gpt4 key购买 nike

我有一个包含 2 列纬度和经度的表格,我想对 2 列上的 (just) tandem 值进行 GROUP 精确匹配。
表格行:

/* Points Table */
time |lat |long
113 |2.1 |5.8
114 |2.1 |5.6 -Set as Group
115 |2.1 |5.6 -Set as Group
116 |2.1 |5.6 -Set as Group
117 |2.1 |5.6 -Set as Group
118 |2.3 |5.2
119 |2.4 |5.3
120 |2.5 |5.3 -Set as Group
121 |2.5 |5.3 -Set as Group
122 |2.6 |5.3
123 |2.1 |5.6 -Set as Group
201 |2.1 |5.6 -Set as Group
202 |2.1 |5.6 -Set as Group
203 |2.5 |5.3

结果必须是:

/* Points Table */
time |lat |long
113 |2.1 |5.8
114 |2.1 |5.6 -Grouped as 1 with first tandem time
118 |2.3 |5.2
119 |2.4 |5.3
120 |2.5 |5.3 -Grouped as 1 with first tandem time
122 |2.6 |5.3
123 |2.1 |5.6 -Grouped as 1 with first tandem time
203 |2.5 |5.3

我只想对 Tandem 值进行分组,在上面我们有两个时间串联的 2.1 & 5.6 值和组被拆分。 (测试可以在 http://sqlfiddle.com/#!2/5d196 上工作)。 ;)

最佳答案

您可以使用 PHP 执行此操作:

$query = mysqli_query("SELECT time, lat, `long` FROM Points ORDER BY time");

// output header row
echo str_pad('time', 8) .'|';
echo str_pad('lat', 7) .'|';
echo "long\n";

$prevLat = $prevLong = '';
while ($row = mysqli_fetch_assoc($query)) {
if ($row['lat'] === $prevLat && $row['long'] === $prevLong) {
// if this row's lat and long values are the same as the
// previous row's values, skip this row.
continue;
}

$prevLat = $row['lat'];
$prevLong = $row['long'];

echo str_pad($row['time'], 8) .'|';
echo str_pad($row['lat'], 7) .'|';
echo "{$row['long']}\n";
}

这是一个示例:http://codepad.org/c0SjA058

关于php - 将 2 列中的确切 Tandem 值分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13252936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com