忽略MySQL JOIN查询中的空结果

问题描述:

我有一个MySQL连接查询,该查询很好地运行 (免责声明:摘自教程) :

I have a MySQL join query which works well (disclaimer: taken from tutorial):

<?php
    $connection = mysql_connect("localhost", "root", "test") or die("Error connecting to database");
    mysql_select_db("products90", $connection);
    $result = mysql_query("SELECT * FROM buyers LEFT JOIN products USING (id);", $connection) or die("error querying database");
    $i = 0;
    while($result_ar = mysql_fetch_assoc($result)){
    ?>
    <table>
    <tr <?php if($i%2 == 1){ echo "class='body2'"; }else{echo "class='body1'";}?>>
    <td>
    <?php echo $result_ar['buyer_name']; ?></td>
    <td>
    <?php echo $result_ar['manufacturer']; ?>
    </td>
    <td>
    <?php echo $result_ar['product_name']; ?>
    </td>
    </tr>
    </table>
    <?php
    $i+=1;
    }
    ?>

但是,如果我希望它忽略NULL结果,那么我需要对JOIN做什么,哪种类型合适?

However, if I wanted it to ignore NULL results, what would I need to do with the JOIN, which type is appropriate?

我在Google上进行了查看,但不确定从何处着手.

I had a look on Google, but am not sure where to proceed from here.

当您说结果为空时,表示没有产品?

When you say you have null results, you mean where there are no products?

无论如何,如果您的ID为空,则产品中没有结果,您也可以添加

Anyway, if you have null IDs, therefore, no result in products, you could just also add

where products.id is not null

或者,您可以将联接从左联接更改为仅联接,使其成为内部联接

or, you could change the join from left join to just join, making it an inner join