简体   繁体   English

连接 2 个表 (a, b) 并返回 (b) 中的 NULL 值

[英]Joining 2 tables (a, b) and returning values that are NULL in (b)

I would like to join 2 tables (a, b) and returning values that are NULL in (b).我想加入 2 个表(a,b)并在(b)中返回 NULL 的值。 I'm working with 2 tables -- called cookies and all_desserts .我正在使用 2 个表 - 名为cookiesall_desserts Cookies exist in all_desserts , but I want to get a table that displays values of all_desserts.names - cookies.names . Cookies 存在于all_desserts中,但我想获得一个显示all_desserts.names - cookies.names值的表。

Essentially, I want to show all the desserts in the desserts table that are NOT cookies.本质上,我想显示甜点表中不是 cookies 的所有甜点。 I think I need to use null here and a right join.我想我需要在这里使用 null 并正确加入。 I would like to call this new table desserts_without_cookies .我想将这个新表命名为desserts_without_cookies

Here is what I have so far (which doesn't work).这是我到目前为止所拥有的(不起作用)。 Am I doing the wrong type of join?我做错了加入类型吗?

FROM cookies LEFT JOIN desserts.cookies ON cookies.cookie_name = desserts.cookies
WHERE desserts.cookies IS NULL```

You can do it with NOT EXISTS :你可以用NOT EXISTS做到这一点:

select ad.*
from all_deserts ad
where not exists (select 1 from cookies c where c.names = ad.names)

A LEFT JOIN would also work: LEFT JOIN也可以:

select ad.*
from all_deserts ad left join cookies c 
on c.names = ad.names
where c.names is null

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

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