查找A中没有关联行的A中的行,而FK在B上? [英] Find rows in A that don't have an associated row in B, where the FK is on B?

查看:38
本文介绍了查找A中没有关联行的A中的行,而FK在B上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做的是

SELECT * FROM a LEFT JOIN b ON b.a_id=a.id WHERE b.id IS NULL

基本上,我正在尝试查找没有关联的 b a 行,其中外键存储在 b .这是执行此操作的正确方法,还是有另一种联接来执行此操作?

Basically, I'm trying to find the rows of a that don't have an associated b, where the foreign key is stored on b. Is this the proper way to do it, or there a different kind of join to do this?

推荐答案

您正在寻找不存在:

SELECT 
  * 
FROM 
  a
WHERE
  NOT EXISTS (SELECT 1 FROM b WHERE a_id = a.id)

b.a_id 上建立索引有助于提高查询的性能.

Having an index on b.a_id helps the performance of this query.

这篇关于查找A中没有关联行的A中的行,而FK在B上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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