我有一个包含以下结构的表:
TBL1
COL1 COL2 COL2
---- ---- ----
A B 1
B C 3
A C 11
A D 13
SELECT TBL1.COL1, COL2 FROM TBL1
INNER JOIN (SELECT COL1, count(COL1) as CNT FROM TBL1
GROUP BY COL1 HAVING CNT > 1) dup ON TBL1.COL1= dup.COL1 ORDER BY COL1;
此查询回复了这个:
COL1 COL2 COL2
---- ---- ----
A B 1
A C 11
A D 13
我想要结合匹配记录。如果您还没有理解我,我想要展示的结果是:
------------------------
RESULT
------------------------
A -> B 1
C 11
D 13
对于A,具有以下值B 1,C 11和D 13
如果我使用FOREACH,结果是:
------------------------
RESULT
------------------------
A -> B 1
A -> C 11
A -> D 13
提前致谢!
答案 0 :(得分:2)
您应该在foreach中跟踪COL1的当前值,如下所示:
$col1 = null;
foreach(mysql_fetch_assoc($res) as $row) {
if(is_null($col1) || $row['COL1'] != $col1) {
echo $row['COL1'] . " -> ";
$col1 = $row['COL1'];
} else {
echo " ";
}
echo $row['COL2'] . " " . $row['COL3'] . "\n";
}