组精确串联值来自2列?

时间:2012-11-06 14:19:13

标签: php mysql

我有一个包含2列纬度和经度的表格,并希望在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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

试试这个:

SELECT time, lat, long FROM Points GROUP BY lat, long