简体   繁体   English

PHP MYSQL 显示表 a 中的所有值,但只显示表 b 中的匹配值,其中表 b 是单独的循环

[英]PHP MYSQL display all values from table a but only matching values from table b where table b is separate loop

I'm struggeling to get the corresponding values from table b using while loops.我正在努力使用 while 循环从表 b 中获取相应的值。 I have the following data in my database:我的数据库中有以下数据:

Table A表A

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

Table B表B

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

Now, I need the data to output the following table, where the first column is from table a and the next columns are populated from table b:现在,我需要数据来输出下表,其中第一列来自表 a,下一列来自表 b:

ent1 - 10 - 20 - 30 ent1 - 10 - 20 - 30

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

I have tried combining two WHILE loops, but with no success:我尝试结合两个 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'];

    }
}

The result I'm getting is not the one I want stated above because the result does not separate the "entity" field in table b.我得到的结果不是我上面想要的结果,因为结果没有将表 b 中的“实体”字段分开。 How can I alter my script to get the desired result?如何更改我的脚本以获得所需的结果?

You simply need to echo things in a slighly different place你只需要在一个稍微不同的地方回声

$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>';
}

Cue: This is where JOINS join us in this endeavor .提示:这就是 JOINS 加入我们这一努力的地方 BA DUM TSSSS巴达姆 TSSSS

You can use the ANSI syntax or the traditional where clause , they both work the same.您可以使用ANSI 语法或传统的 where 子句,它们的工作方式相同。

In your case, you could write something like..在你的情况下,你可以写一些类似..

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

Now you have all the rows from tableA and respectively related rows from tableB and lets say that you have fetched all the result inside the array variable named.... umm.... $result .现在您拥有来自 tableA 的所有行和来自 tableB 的相关行,并且假设您已获取名为.... umm.... $result的数组变量中的所有$result

Now, All you need is a little metaphorical sleight of hand in php as below...现在,您所需要的只是 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);

?>

let me write you a little html too.让我也给你写一点 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>

...and voila! ……瞧!

NOTE:- This is totally something which can be called a 'blind code' and I didn't run it.注意:-这完全是可以称为“盲码”的东西,我没有运行它。 But this should be enough to point you in the right direction.但这应该足以为您指明正确的方向。

You can generate the data entirely in one query.您可以在一个查询中完全生成数据。 This way you can simplify your PHP to one while loop:通过这种方式,您可以将 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

Output:输出:

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

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM