简体   繁体   English

左联接不返回值

[英]Left join not returning values

This query is giving values as null for accessories_type.type but this table has values: 该查询为accessories_type.type提供的值为null ,但此表具有以下值:

SELECT accessories.id, accessories.name, accessories_type.type
FROM accessories
LEFT JOIN accessories_type ON accessories_type.type = accessories.accessories_type_id;

Your query is a little strange. 您的查询有点奇怪。 The value accessories_type.type in the select clause is the same as being used for the join. select子句中的值accessories_type.type与用于联接的值相同。

Assuming there are no duplicate rows in the type table, you can get the same effect by doing: 假设类型表中没有重复的行,则可以通过执行以下操作获得相同的效果:

select id, name, accessories_type_id
from accessories

Your queries is additionally looking for matches in the second table. 您的查询还在第二张表中寻找匹配项。 When there are no matches, you are getting NULL. 没有匹配项时,您将得到NULL。

Normally, I would expect something like accessories_type.typename in the select . 通常,我希望在select一些类似accessories_type.typename东西。 This gives me an idea. 这给了我一个主意。 Perhaps you really mean: 也许您真的是说:

SELECT accessories.id, accessories.name, accessories_type.type
FROM accessories
LEFT JOIN accessories_type ON accessories_type.id = accessories.accessories_type_id;

That is, you should be joining on the id . 也就是说,您应该加入id You are joining on the name and there is no match (in general). 您正在加入该name并且没有匹配项(通常)。

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

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