简体   繁体   English

忽略 null 导致 MySQL JOIN 查询

[英]Ignore null results in MySQL JOIN queries

I have a MySQL join query which works well (disclaimer: taken from tutorial) :我有一个运行良好的 MySQL 连接查询(免责声明:取自教程)

<?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;
    }
    ?>

However, if I wanted it to ignore NULL results, what would I need to do with the JOIN, which type is appropriate?但是,如果我希望它忽略 NULL 结果,我需要对 JOIN 做什么,哪种类型合适?

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?当你说你有 null 结果时,你的意思是那里没有产品?

Anyway, if you have null IDs, therefore, no result in products, you could just also add无论如何,如果您有 null ID,因此,没有结果产品,您也可以添加

where products.id is not null

or, you could change the join from left join to just join, making it an inner join或者,您可以将连接从左连接更改为仅连接,使其成为内部连接

SELECT * FROM buyers LEFT JOIN products USING (id) WHERE products.id IS NOT NULL

OR或者

SELECT * FROM buyers JOIN products USING (id)

Using JOIN only brings matched rows where as the LEFT JOIN brings all buyers regardless of any matches found in products and filters the records afterwards.使用JOIN只会带来匹配的行,因为LEFT JOIN会带来所有买家,而不管在产品中找到任何匹配项,然后过滤记录。 I am not exactly sure about your scenario but it seems simpler to me to just use JOIN instead of LEFT JOIN我不太确定您的情况,但对我来说只使用JOIN而不是LEFT JOIN似乎更简单

By saying you want to ignore NULL results what exactly do you mean?说你想忽略 NULL 结果到底是什么意思? Are you saying that when your left join executes you're getting some buyers which NULL next to them where products would be (had they bought some)?你是说当你的左连接执行时你会得到一些买家,他们旁边的 NULL产品会在哪里(他们买了一些)?

In that case what you're looking for is an INNER JOIN which will only show values if it has a match in the other .在这种情况下,您正在寻找的是一个 INNER JOIN ,如果它在 other 中有匹配项,它只会显示值

You would use an INNER JOIN for that (if I got your question right).您将为此使用 INNER JOIN(如果我的问题正确)。

For an overview of JOIN types in MySQL please refer to http://dev.mysql.com/doc/refman/5.1/en/join.html For an overview of JOIN types in MySQL please refer to http://dev.mysql.com/doc/refman/5.1/en/join.html

If you want to ignore the null results then just use the INNER JOIN , you don't need a LEFT JOIN .如果您想忽略 null 结果,则只需使用INNER JOIN ,您不需要LEFT JOIN
Change your query to:将您的查询更改为:

SELECT * FROM buyers JOIN products USING (id)

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

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