正确使用“ NOT IN”的方式Postgres [英] Correct way to use "NOT IN" Postgres

查看:874
本文介绍了正确使用“ NOT IN”的方式Postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个桌子,人和车辆。车辆属于人。我试图检查一个人是否没有车辆。我试图通过加入人员和车辆并显示不位于Vehicles.person_id中的人员ID来做到这一点。

I have two tables, People, and Vehicles. Vehicles belongs to people. Im trying to check if a person does not have a vehicle. I was attempting to do this by joining People and Vehicles, and displaying the persons ID that is NOT IN Vehicles.person_id.

这什么也没返回,我想知道是否我做错了什么,或者是否有更有效的方法。

This is returning nothing, and has me wondering if there is something I did wrong, or if there is a more efficient way of doing this.

查询低于

从People
中选择People.id
内联车辆People.id = Vehicles.person_id
,其中People.id不在Vehicles.person_id中;

推荐答案

使用左联接找出没有车辆的人

Use left join to figure out the persons with no vehicles

  Select distinct People.id 
  From People 
  LEFT JOIN Vehicles on        People.id=Vehicles.person_id 
  where Vehicles.person_id is NULL

这篇关于正确使用“ NOT IN”的方式Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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