I have an MySQL database table that is created like this
CREATE TABLE table1 (
column1 varchar(50),
column2 varchar(50),
column3 int(3)
);
INSERT INTO table1
VALUES ("column1 value1", "column2 value1", 120),
("column1 value1", "column2 value1", 240),
("column1 value2", "column2 value1", 240),
("column1 value2", "column2 value2", 10);
On this table. I execute this SQL query:
SELECT column1, column2, SUM(column3) FROM table1 GROUP BY column1, column2
Is it possible to fetch the result into a multidimensional associative array with column1 as the "level 1" key, and column2 & column3 as a key-value pair?
Desired result:
Array
(
[column1 value] => Array
(
[column2 value] => column3 value
)
)
I tried using PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_ASSOC as the fetch style argument, but that resulted in:
Array
(
[column1 value] => Array
(
[column2 key] => column2 value
[column3 key] => column3 value
)
)
This is how I am executing my query and fetching the result:
$stmt = $pdo->prepare("SELECT column1, column2, SUM(column3) FROM table GROUP BY column1, column2");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_ASSOC);
Is there a fetch style combination that will get me the desired result, or will I have to do this programmatically?