【问题标题】:PHP MYSQL display all values from table a but only matching values from table b where table b is separate loopPHP MYSQL 显示表 a 中的所有值,但仅显示表 b 中的匹配值,其中表 b 是单独的循环
【发布时间】:2018-08-18 14:44:20
【问题描述】:

我正在努力使用 while 循环从表 b 中获取相应的值。我的数据库中有以下数据:

表 A

数字 - 实体
3000 - ent1
3010 - ent1
4000 - ent1

表 B

数字 - 实体
3000 - 10
3010 - 10
3010 - 20
4000 - 20
3000 - 30
4000 - 30

现在,我需要数据来输出下表,其中第一列来自表 a,接下来的列来自表 b:

ent1 - 10 - 20 - 30

3000 - 3000 - 空 - 3000
3010 - 3010 - 3010 - 空
4000 - 空 - 4000 - 4000

我尝试过组合两个 WHILE 循环,但没有成功:

$query_entity = "SELECT number, entity FROM table_a ORDER BY number ASC";
$result_entity = mysqli_query($mysqli, $query_entity);

while ($entities = mysqli_fetch_array($result_entity)) {
    $entitiesAccount = $entities['number'];

    $query_entity_tabtwo = "SELECT number, entity 
                            FROM table_b 
                            WHERE number = $entitiesAccount";
    $result_entity_tabtwo = mysqli_query($mysqli, $query_entity_tabtwo);

    while ($entities_tabtwo = mysqli_fetch_array($result_entity_tabtwo)) {

        echo $entitiesAccount . " - " . $entities_tabtwo['number'];

    }
}

我得到的结果不是我想要的上述结果,因为结果没有分隔表 b 中的“实体”字段。如何更改我的脚本以获得所需的结果?

【问题讨论】:

  • 我需要数据来输出下表 你的意思是需要渲染一个HTML表格吗?导致获取一组数据并渲染它可能是两个不同的进程,每个进程都有完全不同的方法。
  • 9KSoft;是的,请考虑以上示例作为数据的填充方式。

标签: php mysqli while-loop


【解决方案1】:

你只需要在一个稍微不同的地方呼应一些东西

$sql = "SELECT number, entity 
        FROM table_a 
        ORDER BY number ASC";
$result = mysqli_query($mysqli, $sql);

while ($row = mysqli_fetch_array($result_entity)) {
    $entitiesAccount = $row['number'];

    $sql = "SELECT number, entity 
            FROM table_b 
            WHERE number = $entitiesAccount";
    $result2 = mysqli_query($mysqli, $sql);

    echo $entitiesAccount;

    while ($row2 = mysqli_fetch_array($result2)) {

        echo " - " . $row2['number'];

    }
    echo '<br>';
}

【讨论】:

    【解决方案2】:

    提示:这是 JOINS 与我们一起努力的地方。 BA DUM TSSSS

    您可以使用ANSI syntax or the traditional where clause,它们的工作方式相同。

    在你的情况下,你可以写类似..

    SELECT ta.number, tb.entity
    FROM tableA as ta
    LEFT JOIN tableB as tb ON tb.number = ta.number
    WHERE ta.entity = 'ent1'; // I suppose this is where you do the selection
    

    现在您拥有了 tableA 中的所有行以及 tableB 中的相关行 假设您已经在名为.... umm.... $result 的数组变量中获取了所有结果。

    现在,您所需要的只是在 php 中进行一些隐喻性的花招,如下所示...

    <?php
    $result      = []; // This comes from the mysql
    $output      = [];
    $columns_raw = [];
    $entity_name = 'ent1'; // This comes from your selection logic the same that goes to sql.
    foreach ($result as $row) {
        $columns_raw[]                            = $row['entity'];
        $output[$row['number']][$row['entity']][] = $row;
    }
    $columns = array_unique($columns_raw);
    
    ?>
    

    让我也给你写一点html。

    <table>
        <thead>
        <th><?php echo $entity_name; ?></th>
        <?php foreach ($columns as $column) { ?>
            <th><?php echo $column; ?></th>
        <?php } ?>
    </thead>
    <tbody>
        <?php foreach ($output as $number => $row) { ?>
            <tr><?php echo $number; ?></tr>
            <tr><?php
                foreach ($columns as $column) {
                    if (array_key_exists($column, $row)) {
                        echo $number;
                    } else {
                        echo 'null';
                    }
                }
                ?></tr>
        <?php } ?>
    </tbody>
    </table>
    

    ...瞧!

    注意:- 这完全可以称为“盲码”,我没有运行它。但这应该足以为您指明正确的方向。

    【讨论】:

      【解决方案3】:

      您可以在一个查询中完全生成数据。这样您就可以将 PHP 简化为一个 while 循环:

      SELECT a.number AS ent1, 
          GROUP_CONCAT(CASE WHEN b.entity = 10 THEN b.number END) AS `10`,
          GROUP_CONCAT(CASE WHEN b.entity = 20 THEN b.number END) AS `20`,
          GROUP_CONCAT(CASE WHEN b.entity = 30 THEN b.number END) AS `30`
      FROM table_a a
      JOIN table_b b ON b.number = a.number
      GROUP BY ent1
      

      输出:

      ent1    10      20      30
      3000    3000    (null)  3000
      3010    3010    3010    (null)
      4000    (null)  4000    4000
      

      Demo

      【讨论】:

      • 看起来这可能只是对一个所有实体都预先知道的小表起作用,但这个查询会根据实体编号限制输出。假设表中填充了数百个实体,有没有办法让“group_concat”包含所有值而不在查询中硬编码它们?
      • 是的,我只是根据您的问题提出的。如果您可以了解更多数据可能是什么样子,我可以弄清楚查询是否可以调整。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      相关资源
      最近更新 更多