简体   繁体   中英

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.

The problem is, that the data returned on the left does not include the rows that matches the right.

Left table:

b_kategori

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

b_underkategori

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

The SQL looks like this:

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?

The above SQL is stripped down to a minimum so I can debug it. The "original" also has a WHERE statement. 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 ,我认为会得到您想要的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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