sql group by正在复制结果

时间:2015-01-22 09:42:29

标签: php mysql sql group-by

我试图创建一个带有计数的制造商列表。

这是我的查询

$query = $this->pdo->prepare('SELECT manufacture, count(*) AS count
                                      FROM listed_watches
                                      GROUP BY manufacture');

但是当我做print_r时,它会重复结果。它显示“制造”和“计数”,但它也显示[0]和[1],为什么会这样?

我只是希望它显示[制造],[计数]

Array
(
    [0] => Array
        (
            [manufacture] => Audemars Piguet
            [0] => Audemars Piguet
            [count] => 2
            [1] => 2
        )

    [1] => Array
        (
            [manufacture] => Bell and Ross
            [0] => Bell and Ross
            [count] => 3
            [1] => 3
        )

    [2] => Array
        (
            [manufacture] => Bulova
            [0] => Bulova
            [count] => 1
            [1] => 1
        )

)

1 个答案:

答案 0 :(得分:1)

试试这个:

$result = $query->fetchAll(PDO::FETCH_ASSOC);

默认情况下,您可以在初始化连接时使用此提取方法:

$pdo_options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;