DCR DCR - 1 year ago 60
MySQL Question

pulling data out of mysql creating array with array structure

I have a MySql database with three columns: skp, date and usr. For each skp and date pair there are several usr(s). Is there a query that will pull the data out with the following array structure:


I'm wondering if somehow using a group by would work?

Answer Source


SELECT skp, date, GROUP_CONCAT('usr') AS usr
FROM yourTable
GROUP BY skp, date

Then when retrieving, you can split the usr column into an array.

$array = ();
while ($row = mysqli_fetch_assoc($result)) {
    $row['usr'] = explode(',', $row['usr']);
    $array[] = $row;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download