MYSQL 外连接,不返回不匹配的行

发布于 2024-12-22 05:48:33 字数 2934 浏览 4 评论 0原文

我的数据库中有这两个表

describe external_review_sources;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| ersID | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50)  | NO   | UNI | NULL    |                |
| logo  | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

describe listing_external_review_source_rel;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| lersrID | int(11)      | NO   | PRI | NULL    | auto_increment |
| bid     | int(10)      | NO   |     | NULL    |                |
| url     | varchar(255) | NO   |     | NULL    |                |
| ersID   | int(11)      | YES  |     | NULL    |                |
| active  | int(10)      | NO   |     | NULL    |                |
| order   | int(10)      | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

我以这种方式查询这些表:

    SELECT * 
    FROM 
        listing_external_review_source_rel 
        RIGHT JOIN
        external_review_sources USING(ersID)
    where bid=902028 or bid IS NULL;

+-------+---------------+------+---------+--------+-------+--------+-------+
| ersID | name          | logo | lersrID | bid    | url   | active | order |
+-------+---------------+------+---------+--------+-------+--------+-------+
|     1 | G1            | a    |      17 | 902028 | url11 |      1 |     0 |
|     2 | D1            | b    |      18 | 902028 | url22 |      0 |     0 |
+-------+---------------+------+---------+--------+-------+--------+-------+
2 rows in set (0.00 sec)

正如您所看到的,显示的结果为 bid=902028,但是对于诸如 866696 之类的出价,该出价在listing_external_review_source_rel中不存在,

SELECT * 
FROM listing_external_review_source_rel 
     RIGHT JOIN external_review_sources USING(ersID) 
where bid=866696  or bid IS NULL;
Empty set (0.00 sec)

我希望 结果为空结果是这样的:

+-------+---------------+------+---------+--------+-------+--------+-------+
| ersID | name          | logo | lersrID | bid    | url   | active | order |
+-------+---------------+------+---------+--------+-------+--------+-------+
|     1 | G1            | NULL |     NULL| NULL   | NULL  |   NULL |  NULL |
|     2 | D1            | NULL |     NULL| NULL   | NULL  |   NULL |  NULL |
+-------+---------------+------+---------+--------+-------+--------+-------+
2 rows in set (0.00 sec)

这就是我使用的“或出价为空”条件。

我做错了什么以及什么查询会给我这个结果?我基本上也对结果中存在不匹配的行感兴趣。

I have these two tables in my db

describe external_review_sources;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| ersID | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50)  | NO   | UNI | NULL    |                |
| logo  | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

And

describe listing_external_review_source_rel;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| lersrID | int(11)      | NO   | PRI | NULL    | auto_increment |
| bid     | int(10)      | NO   |     | NULL    |                |
| url     | varchar(255) | NO   |     | NULL    |                |
| ersID   | int(11)      | YES  |     | NULL    |                |
| active  | int(10)      | NO   |     | NULL    |                |
| order   | int(10)      | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

I query these tables this way:

    SELECT * 
    FROM 
        listing_external_review_source_rel 
        RIGHT JOIN
        external_review_sources USING(ersID)
    where bid=902028 or bid IS NULL;

+-------+---------------+------+---------+--------+-------+--------+-------+
| ersID | name          | logo | lersrID | bid    | url   | active | order |
+-------+---------------+------+---------+--------+-------+--------+-------+
|     1 | G1            | a    |      17 | 902028 | url11 |      1 |     0 |
|     2 | D1            | b    |      18 | 902028 | url22 |      0 |     0 |
+-------+---------------+------+---------+--------+-------+--------+-------+
2 rows in set (0.00 sec)

As you can see results are showing up for bid=902028, how ever for a bid such as 866696 that does NOT exist in listing_external_review_source_rel, the results are empty

SELECT * 
FROM listing_external_review_source_rel 
     RIGHT JOIN external_review_sources USING(ersID) 
where bid=866696  or bid IS NULL;
Empty set (0.00 sec)

I expect the results to be this:

+-------+---------------+------+---------+--------+-------+--------+-------+
| ersID | name          | logo | lersrID | bid    | url   | active | order |
+-------+---------------+------+---------+--------+-------+--------+-------+
|     1 | G1            | NULL |     NULL| NULL   | NULL  |   NULL |  NULL |
|     2 | D1            | NULL |     NULL| NULL   | NULL  |   NULL |  NULL |
+-------+---------------+------+---------+--------+-------+--------+-------+
2 rows in set (0.00 sec)

That's what I have used the "or bid IS NULL" condition.

What am I doing wrong and what query would give me this result? I basically am interested in having nonmatching rows in my results as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稳稳的幸福 2024-12-29 05:48:33

大多数人使用 LEFT JOIN,因此我将其重写为更标准:

SELECT *
FROM external_review_sources a LEFT JOIN
     listing_external_review_source_rel b ON a.ersID=b.ersID AND bid=866696;

请记住,外部联接返回 ON 条件匹配的所有行,而在不匹配的情况下返回 NULL。在这种情况下,您的匹配条件不仅仅是 ersID

Most people use LEFT JOIN so I'll rewrite it to be more standard:

SELECT *
FROM external_review_sources a LEFT JOIN
     listing_external_review_source_rel b ON a.ersID=b.ersID AND bid=866696;

Remember that an outter join returns all rows where the ON condition matches, and NULL where they don't. In this case your match condition is more than just the ersID

爱她像谁 2024-12-29 05:48:33

尝试一下

SELECT * FROM 
    external_review_sources e
    LEFT JOIN
    listing_external_review_source_rel r ON e.ersID = r.ersID AND r.bid = 866696
WHERE
    r.bid IS NULL;

在“外部表”上进行过滤时,过滤器需要是派生表或在 JOIN 中,因为您希望在 WHERE 之前(逻辑上)进行过滤。此外,为了清晰起见,最佳实践是使用 LEFT JOIN。

使用派生表

SELECT * FROM 
    external_review_sources e
    LEFT JOIN
    (
     SELECT * 
     FROM listing_external_review_source_rel
     WHERE bid = 866696
    ) r USING (ersID)
WHERE
    r.bid IS NULL;

Try this

SELECT * FROM 
    external_review_sources e
    LEFT JOIN
    listing_external_review_source_rel r ON e.ersID = r.ersID AND r.bid = 866696
WHERE
    r.bid IS NULL;

When filtering on "outer tables", the filter needs to be a derived table or in the JOIN because you want to filter before the WHERE (logically). Also, a best practice is to use LEFT JOIN for clarity.

With a derived table

SELECT * FROM 
    external_review_sources e
    LEFT JOIN
    (
     SELECT * 
     FROM listing_external_review_source_rel
     WHERE bid = 866696
    ) r USING (ersID)
WHERE
    r.bid IS NULL;
饮惑 2024-12-29 05:48:33

尝试这种方式,将 bid 列的测试从 WHERE 子句移到 JOIN 中:

SELECT * 
    FROM listing_external_review_source_rel ler
        RIGHT JOIN external_review_sources ers   
            ON ler.ersID = ers.ersID
                AND ler.bid=866696;

Try it this way, moving the test of the bid column out of the WHERE clause and into the JOIN:

SELECT * 
    FROM listing_external_review_source_rel ler
        RIGHT JOIN external_review_sources ers   
            ON ler.ersID = ers.ersID
                AND ler.bid=866696;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文