gpt4 book ai didi

PHP group mysql select by 两列

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:42 26 4
gpt4 key购买 nike

我在 MySQL 中有这个表:

table

我想按浏览器和版本对选择的结果进行分组。

想要的输出:

Array
(
[firefox 3.0] => 2
[chrome 24.0] => 1
[ie 9.0] => 2
[ie 10.0] => 1
)

问题:如何分组选择得到想要的输出?

注意:表格可以包含更多版本的浏览器,因此数组会扩展。

我的尝试:

PHP:

public function get_browsers() {
$query = "
SELECT browser, COUNT(browser) AS num
FROM table
GROUP BY browser
ORDER BY browser ASC
";
$select = mysqli_query($this->c, $query);
while ($row = mysqli_fetch_array($select)) {
$data[$row['browser']] = (int) $row['num'];
}
return $data;
}

当前输出:

Array
(
[firefox] => 2
[chrome] => 1
[ie] => 3
)

最佳答案

public function get_browsers() {
$query = '
SELECT browser, version, COUNT(*) AS num
FROM table
GROUP BY browser, version
';
$select = mysqli_query($this->c, $query);
while ($row = mysqli_fetch_array($select)) {
$data[$row['browser'].' '.$row['version']] = (int) $row['num'];
}
return $data;
}

关于PHP group mysql select by 两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21400606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com