简体   繁体   English

MySQL JOIN:仅返回右表中所有值均为 NULL 的行

[英]MySQL JOIN: Only return rows where all values from right table are NULL

I have two MySQL tables like this:我有两个这样的 MySQL 表:

orders:
+----------+------+
| order_id | name |
+----------+------+
| 1        | Mary |
| 2        | John |
| 3        | Anne |
+----------+------+

order_details:
+----------+----------------+
| order_id | shipped        |
+----------+----------------+
| 1        | null           |
| 1        | null           |
| 1        | SHIPPED-123ABC |
| 2        | null           |
| 2        | null           |
| 3        | SHIPPED-XYZ    |
| 3        | SHIPPED-XYZ    |
| 3        | null           |
+----------+----------------+

Now I want to join the order_details-table to the orders-table but only show the entries that only have NULL shipped values in the order_details table.现在我想将 order_details-table 加入到 orders-table 但只显示 order_details 表中只有 NULL 运输值的条目。 So in this example only order_id #2 would get returned.所以在这个例子中,只有 order_id #2 会被返回。

So the result would be:所以结果将是:

+----------+------+---------+
| order_id | name | shipped |
+----------+------+---------+
| 2        | John | null    |
+----------+------+---------+

Thank you!谢谢!

You can use not exists :您可以使用not exists

select o.order_id, o.name, null as shipped 
from orders o
where not exists (select 1 
                  from order_details od 
                   where od.order_id = o.order_id and od.shipped is not null
                 );

Simple aggregation with JOIN would also work :使用JOIN简单聚合也可以:

select o.order_id, o.name, null as shipped
from orders o inner join
     order_details od
     on od.order_id = o.order_id
group by o.order_id, o.name
having min(od.shipped) = max(od.shipped) and min(od.shipped) is null;

If you want orders that have at least one row in order_details , then use aggregation:如果您想要在order_details中至少一行的订单,请使用聚合:

select o.*
from (select od.order_id
      from order_details 
      group by od.order_id
      having count(shipped) = 0
     ) od join
     orders o
     on od.order_id = o.order_id;

If you want all such orders, even those with no rows in order_details , then use not exists :如果你想要所有这样的订单,即使是那些在order_details没有行的订单,那么使用not exists

select o.*
from orders o
where not exists (select 1
                  from order_details od
                  where od.order_id = o.order_id and
                        od.shipping is not null
                 );

Check whether below query gives what you want.检查以下查询是否提供您想要的。

SELECT
  o.order_id,
  o.name,
  od.shipped
FROM
  orders o
  INNER JOIN order_details od ON od.order_id = o.order_id
WHERE
  od.shipped IS NULL
  AND od.shipped NOT IN     
(
SELECT od.shipped
WHERE
    od.shipped IS NOT NULL
     )

暂无
暂无

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

相关问题 MySQL Join - 仅当所有右表行都满足 WHERE 子句时才检索左表行 - MySQL Join - Retrieve Left Table Rows only if all the Right Table Rows satisfies the WHERE clause 左外部联接,带选择,其中右表中的值不返回左中的所有行 - Left outer join with select where value in right table does not return all rows from left 如何仅返回在 mysql 连接中找到所有提供的值的行? - How to return rows only where all supplied values are found in a mysql join? MySQL:仅返回一个表中的行,其中另一个表的一列中的所有值都相同 - MySQL: Return only rows in one table where ALL values in one column of another table are the same 使用JOIN和WHERE返回一个表中的所有行,而返回另一个表中的仅少数行 - Return all rows from one table and only a few from another with JOIN and WHERE MySQL-仅当存在WHERE子句中的所有值时才返回行 - MySQL - Return rows only if all the values in WHERE clause are present left join不返回mysql中左表的所有行 - left join not return the all the rows from left table in mysql 如何连接表以返回所有正确的行,以及另外一个空行? - How can I join a table to return all rows right and in addition a null row? 返回所有父表行,但对于与WHERE子句与MYSQL不匹配的子表行返回null - Returning all parent table rows but return null for child table rows that don't match the WHERE clause with MYSQL sql右联接从右表返回多行 - sql right join return multiple rows from right table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM