我有一个具有两列纬度和经度的表,希望对两列上的完全匹配(只是)串联值进行分组。
表行:
/* 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
我只想分组串联的值,在上面我们有两个时间串联的
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列中的精确串联值分组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13252936/