2

我有一个包含 2 列纬度和经度的表,并且想要 GROUP 精确匹配(仅) 2 列上的 串联值。
表行:

/* 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上工作)。;)

4

2 回答 2

2

您可以使用 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

于 2012-11-06T14:24:07.780 回答
0

试试这个:

SELECT time, lat, long FROM Points GROUP BY lat, long
于 2012-11-06T14:39:03.713 回答