简体   繁体   English

SQL Left Join不返回第一个匹配项

[英]SQL Left Join not returning first match

I have 2 tables where I would like to return all data in the left table and the matching data in the right. 我有2个表,我想在其中返回左侧表中的所有数据,并在右侧返回匹配的数据。

The problem is, that the data returned on the left does not include the rows that matches the right. 问题是,左侧返回的数据不包括与右侧匹配的行。

Left table: 左表:

b_kategori b_kategori

id, kat, navn
32, 1, 'Bits'
33, 2, 'Donkrafte'
34, 3,'Dornsæt'
35, 4, 'Hammere'

b_underkategori b_underkategori

id, kat, navn
26, 1, 'Skrueudtrækkertoppe'
27, 1, 'WIHA Bits'

The SQL looks like this: SQL看起来像这样:

SELECT k.kat, k.navn as navnet, u.navn as unavn FROM b_kategori k LEFT OUTER JOIN b_underkategori u ON k.kat = u.kat 

I would expect the SQL to return all rows on the left (b_kategori) and also all rows on the right (b_underkategori), but I am missing the b_kategori 32, 1, 'Bits' Is this excluded because it matches the right table data, or is it possible to get the SQL to return all data? 我希望SQL能够返回左侧的所有行(b_kategori)和右侧的所有行(b_underkategori),但是我缺少b_kategori 32,1 32, 1, 'Bits'是否排除在外,因为它与正确的表数据匹配,还是有可能让SQL返回所有数据?

The above SQL is stripped down to a minimum so I can debug it. 上面的SQL被精简到最低限度,因此我可以对其进行调试。 The "original" also has a WHERE statement. “原始”还具有WHERE语句。 If I can get the above to work I believe I also can get the original to work. 如果我可以使以上各项起作用,我相信我也可以使原件起作用。 :-) :-)

I hope someone can guide me in the right direction. 我希望有人可以指引我正确的方向。

Thanks in advance. 提前致谢。

检查Left join相交 ,如果您的左表在右表上没有匹配项,它将切掉该部分,将其替换为FULL JOIN ,我认为会得到您想要的。

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

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