I need help with DISTINCT. I would like to display Distinct row but display also all rows
Example this table from database:
+----+-----+-----+
|col1|col2 |col3 |
+----+-----+-----+
|A |one |two |
|A |three|four |
|A |five |six |
|B |seven|eight|
|B |nine |ten |
+----+-----+-----+
I would like the display to look like this :
A
one |two
three|four
five |six
B
seven|eight
nine |ten
Can anyone help?
解决方案
The easiest way would be to fetch all rows from the database, and then group them in PHP.
// Querying:
$query = mysql_query('select * from tbl');
$results = array(); // Store all results in an array, grouped by col1
while($row = mysql_fetch_assoc($query)) {
$col1 = $row['col1'];
// This is basically grouping your rows by col1
if(!isset($results[$col1]))
$results[$col1] = array();
$results[$col1][] = $row;
}
// Displaying:
foreach($results as $col1 => $rows) {
echo "
" . $col1 . "
";foreach($rows as $row) {
echo $row['col2'] . "|" . $row['col3'] . "
";
}
}
Yields:
A
one |two
three|four
five |six
B
seven|eight
nine |ten
Note that I use the deprecated mysql_functions just for simplicity, do not use them in production.