简体   繁体   中英

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. I have the following data in my database:

Table A

number - entity
3000 - ent1
3010 - ent1
4000 - ent1

Table B

number - entity
3000 - 10
3010 - 10
3010 - 20
4000 - 20
3000 - 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:

ent1 - 10 - 20 - 30

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

I have tried combining two WHILE loops, but with no success:

$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. 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 . BA DUM TSSSS

You can use the ANSI syntax or the traditional where clause , they both work the same.

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 .

Now, All you need is a little metaphorical sleight of hand in php as below...

<?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.

<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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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