php - 计算数据表/数据库中某些输入的出现次数

标签 php html mysql

我目前正在根据存储在 mysql 数据库中的信息填充网站上的数据表

表格的形式为:

Table 1                                     

 ID | DataA | Other Info | Count Data                
 ======================================     
  1 |   A   | Other Info |      2          
  2 |   B   | Other Info |      3                     

Table 2        

ID | Input 
==========
 1 |   A
 2 |   A
 3 |   B
 4 |   B
 5 |   B

Count data in table 1旨在显示 entry form the DataA 出现的频率列出现在input column in Table2

更新....

CREATE TABLE driver   (
  `ID` INTEGER,
  `DataA` VARCHAR(1),
  `Other Info` VARCHAR(10),
   primary key (ID)
);

$sql = "SELECT * FROM `driver` ORDER BY drivercompany";
$query = mysqli_query($conn, $sql);

if (mysqli_num_rows($query) > 0) {
    // output data of each row
    while ($result = mysqli_fetch_assoc($query)) {
        echo "<tr>
    <td class='class'>" . $result['id'] . "</td>
        <td>" . $result['DataA'] . "</td>                
        <td>" . $result['otherinfo'] . "</td>
        <td></td> /**count goes here**/
        </tr>";
    }
} 
?>

使用db fiddle,我需要获取计数的查询是:

SELECT driver.*, COUNT(add_job.Input) AS counter
FROM driver
LEFT JOIN add_job ON add_job.Input = driver.DataA
GROUP BY driver.ID

我尝试将其添加到我的代码中,如下所示:

$sql = "SELECT * FROM `driver` ORDER BY drivercompany";
$query = mysqli_query($conn, $sql);
$count = "SELECT `driver`.*, COUNT(`add_job`.`adddriver`) AS counter 
        FROM `driver` 
        LEFT JOIN `add_job` ON `add_job`.`adddriver` = `driver`.`drivercompany` 
        GROUP BY `driver`.`id`";
$counter = mysqli_query($conn, $count);

while($result = mysqli_fetch_assoc($query,$counter)) { //and other variations

我无法确定如何让 $count 和 $query 一起工作

最佳答案

您可以连接两个表,按 table1 的主键分组,并使用单个查询计算 table2 中出现的次数。

假设 IDtable1 中的主键,请将查询更改为:

$sql = "
    SELECT t1.*, COUNT(t2.Input) AS counter
    FROM table1 t1
    LEFT JOIN table2 t2 ON t2.Input = t1.DataA
    GROUP BY t1.ID
";

结果将是:

| ID  | DataA | Other Info | Count Data | counter |
| --- | ----- | ---------- | ---------- | ------- |
| 1   | A     | Other Info | 2          | 2       |
| 2   | B     | Other Info | 3          | 3       |

查看db-fiddle中的结果

然后您可以在输出中使用 $result['counter'] 。例如:

echo "
    <tr>
        <td class='class'>{$result['id']}</td>
        <td>{$result['DataA']}</td>                
        <td>{$result['otherinfo']}</td>
        <td>{$result['counter']}</td>
    </tr>
";

注意:如果您在 MySQL < 5.7 或任何版本的 MariaDB 中启用了 ONLY_FULL_GROUP_BY 模式,则查询将引发错误。在这种情况下,您应该将其更改为

SELECT t1.id, t1.DataA, t1.otherinfo, COUNT(t2.Input) AS counter
FROM table1 t1
LEFT JOIN table2 t2 ON t2.Input = t1.DataA
GROUP BY t1.id, t1.DataA, t1.otherinfo

在 GROUP BY 子句中包含所有选定的非聚合列。

关于php - 计算数据表/数据库中某些输入的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57841984/

相关文章:

php - 在多列中搜索多个单词的 SQL 查询

mysql - 将数据从 MySQL 拉入 Hadoop

php - 使用 file_get_content 的发布请求不起作用

php - 链接一个实体以具有多个图像和视频

c# - MVC 3 在 ValidationSummary 中显示 HTML

javascript - 通过在 anchor 标记上应用 onclick 来删除文本

C# - 创建配置文件来保存应用程序中使用的数据

php - 无法使用 PHP 5.5 将数据添加到 webmatrix 3 中的 MySQL 5.7

php - 构建 magento 模块

html - 无法在 HTML 中的垂直菜单后写文章