简体   繁体   English

意外结果:如果 tblA.ID 与 tblB.ID 不匹配,则查询以显示 tblA 记录

[英]Unexpected result: query to show tblA records if tblA.ID doesn't match tblB.ID

I made a query to show tblA records if tblA.ID doesn't match tblB.ID.如果 tblA.ID 与 tblB.ID 不匹配,我进行了查询以显示 tblA 记录。 The query works fine only if there is at least one record in tblB, but if tblB has no records then the query of tblA will return empty, though tblA should return all its records.仅当 tblB 中至少有一条记录时,查询才能正常工作,但如果 tblB 没有记录,则 tblA 的查询将返回空,尽管 tblA 应返回其所有记录。 Below is the code with the unexpected result:以下是具有意外结果的代码:

SELECT tblA.id
FROM tblA INNER JOIN tblB ON tblA.id = tblB.id;

The Unexpected result also occurs in this format: Unexpected 结果也以这种格式出现:

SELECT *
FROM tblA , tblB 
WHERE tblA .id Not Like tblB .id;

Example of the desired an undesired results:想要的和不想要的结果的例子:

在此处输入图片说明

In your case Left join should be used.在您的情况下,应使用左连接。 Try it.尝试一下。

select tbl1.Id
from tbl1 
left join tbl2 on tbl1.Id = tbl2.Id
where tbl2.Id is null

select * from tblA where id in (select id from tblA 除了 select id from tblB )

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

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