sql 分组依据重复结果
sql group by is duplicating results
我正在尝试创建一个带有计数的制造商列表。
这是我的查询
$query = $this->pdo->prepare('SELECT manufacture, count(*) AS count
FROM listed_watches
GROUP BY manufacture');
但是当我这样做时 print_r,它重复了结果。它显示 "manufacture" 和 "count" 但它也显示 [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
)
)
试试这个:
$result = $query->fetchAll(PDO::FETCH_ASSOC);
您可以在初始化连接时默认使用此获取方法:
$pdo_options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
我正在尝试创建一个带有计数的制造商列表。
这是我的查询
$query = $this->pdo->prepare('SELECT manufacture, count(*) AS count
FROM listed_watches
GROUP BY manufacture');
但是当我这样做时 print_r,它重复了结果。它显示 "manufacture" 和 "count" 但它也显示 [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
)
)
试试这个:
$result = $query->fetchAll(PDO::FETCH_ASSOC);
您可以在初始化连接时默认使用此获取方法:
$pdo_options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;