忽略MySQL JOIN查询中的空结果 [英] Ignore null results in MySQL JOIN queries

查看:185
本文介绍了忽略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

这篇关于忽略MySQL JOIN查询中的空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆