tweb tweb - 1 year ago 84
MySQL Question

How can I merge values of existing rows

I have to get all results and push to HTML table, but existing rows have to be combine to 1 row.

This is my table

id domain php_version
1 localhost 5.5.30
2 live 7.05
3 localhost 5.5.30
4 localhost 5.5.30

and the code which output the html table is:

// Prepare query
$stmt = $mysqli->prepare("SELECT * FROM domains ORDER BY domain ASC LIMIT 10");
// Execute the query
// Bind Parameters
$stmt->bind_result($id, $domain, $php_version);

<?php while ($stmt->fetch()) : ?>
<tr class="row-id-<?php echo $id; ?>">
<td class="id"><?php echo $id; ?></td>
<td class="domain"><?php echo $domain; ?></td>
<td class="php_version"><?php echo $php_version; ?></td>
<?php endwhile; ?>

The output looks like this:

enter image description here

And I just want to be like this:

enter image description here

I just want to combine values of dublicated domains in one row/column

Thank you very much!

Answer Source

I Think that I managed to do it with group_concat:

SELECT domain,
       GROUP_CONCAT(DISTINCT php_version SEPARATOR '\n' ) php_versions,
FROM domains GROUP BY domain
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download