简体   繁体   中英

How can I mark the values from one table, that are not in another table (mySQL)?

In mySQL this is my table animals :

+-----------+--------+--------+
| animal_ID |  name  | animal |
+-----------+--------+--------+
|         1 | alan   | dog    |
|         2 | sam    | frog   |
|         3 | marion | cat    |
|         4 | george | rabbit |
|         5 | bob    | bird   |
+-----------+--------+--------+

and this is my table orders

+----------+-----------+
|   date   | animal_ID |
+----------+-----------+
| 02.03.16 |         4 |
| 12.04.16 |         3 |
| 18.07.16 |         1 |
+----------+-----------+

I want to list all animals but mark the animals that are NOT in the orders table red. This is my expected result

 <table > <tr> <td>alan</td> </tr> <tr> <td style="color:red">sam</td> </tr> <tr> <td>marion</td> </tr> <tr> <td>george</td> </tr> <tr> <td style="color:red">bob</td> </tr> </table> 

I have now the problem, that I do not know how to list all animals from the animal table, I just get the animals in my list that are not in the orders table:

$sql = 'SELECT  *  FROM animals a WHERE EXISTS (SELECT * FROM orders o WHERE  o.animal_ID = a.animal_ID)';

Here my result is this:

  <table > <tr> <td>alan</td> </tr> <tr> <td>marion</td> </tr> <tr> <td>george</td> </tr> </table> 

You need to make this script using two query.

 1. Query for all the animals.
 2. Query with the animals ID in the orders.

The first one is:

$sql = 'SELECT  *  FROM animals";

Loop this and check in the order table inside the first loop-

$sql2 = "SELECT * FROM orders WHERE  animal_ID = fetched_animal_ID";
if(mysqli_num_rows($sql2) > 0){
    // print the normalrows here
}else{
    // print the red rows here
}

==============================================================

You can do this using the LEFT JOIN as well-

$sql = "SELECT *, o.animal_ID as o_animal FROM animals a LEFT JOIN orders o 
        ON a.animal_ID = o.animal_ID";

Online Example

Now you need to check the o_animal if it is null or empty then use the red rows and else the normal.

If you have any question please ask me.

You can try another method without using join query.

<?php
include 'conn.php';
$sq=mysqli_query($conn,'select * from orders');
$i=0;
$ar=array();
while($re=mysqli_fetch_array($sq))
{
$anid=$re['animal_ID'];
$ar[$i]=$anid;
$i++;
}
$sql=mysqli_query($conn,"select * from animals");
?>
<table >
    <?php

while($all=mysqli_fetch_array($sql))
{
    if(in_array($all['animal_ID'],$ar))
    {
       ?>

    <tr>

        <td><?php echo $all['name'];?></td>
    </tr
    <?php
    }

else
{
  ?>  <tr>

        <td style="color:red"><?php echo $all['name'];?></td>
    </tr>
<?php
}
}
?>
</table>

Simple negation?

$sql = 'SELECT  *  FROM animals a WHERE NOT EXISTS (SELECT * FROM orders o WHERE  o.animal_ID = a.animal_ID)';

Or maybe grab the count with a sub query, highlight the zeroes...

$sql = 'SELECT  a.*,
        (SELECT COUNT(*) FROM orders WHERE animal_ID = a.animal_ID)
        FROM animals a';

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