简体   繁体   English

具有多个JOIN的SQL查询和比较

[英]SQL Query with multiple JOINs and Comparison

I'm trying to build a query that pulls in data from a few different tables (using JOINs) and I'm just having some issues. 我正在尝试建立一个查询,该查询从几个不同的表中提取数据(使用JOIN),而我只是遇到了一些问题。

Here is the SQL Query... 这是SQL查询...

SELECT Import_Values.id, 
       Import_Values.base_vehicle_id, 
       Import_Values.part_type_id, 
       Import_Values.position, 
       Import_Values.part_id, 
       Import_Values.part_number, 
       Import_Values.brand_id, 
       Import_Values.brand_description, 
       Import_Values.series_id, 
       Import_Values.worldpac_category_id, 
       Import_Values.price, 
       Import_Values.list_price, 
       Import_Values.core, 
       Import_Values.available, 
       Import_Values.weight, 
       Import_Values.height, 
       Import_Values.length, 
       Import_Values.width, 
       Import_Values.selling_increment, 
       Import_Values.popularity, 
       Import_Values.qty, 
       Import_Values.description_line_two, 
       Import_Values.detailed_description, 
       Import_Values.image_url, 
       Import_Values.thumb_url, 
       Import_Values.note, 
       BaseVehicle.YearID, 
       BaseVehicle.MakeID, 
       BaseVehicle.ModelID, 
       Make.MakeName
FROM 
    Import_Values
        INNER JOIN BaseVehicle 
            ON Import_Values.base_vehicle_id=BaseVehicle.BaseVehicleID

After getting the MakeID from the table 'BaseVehicle', I'd like to grab the 'MakeName' which is assigned to the ID but is not in the same table. 从“ BaseVehicle”表中获取MakeID之后,我想获取分配给该ID但不在同一表中的“ MakeName”。 MakeName is within another table called 'Make' and the table structure is like so... MakeName位于另一个名为“ Make”的表中,并且表结构类似于...

[MakeID]  [MakeName]
1         Suzuki
2         Porsche

I thought I'd just do another INNER JOIN like so... 我以为我会像这样再做一次INNER JOIN ...

INNER JOIN Make ON BaseVehicle.MakeID=Make.MakeID

That didn't work, so I'm hoping someone can help me. 那没有用,所以我希望有人可以帮助我。

Your solution should work. 您的解决方案应该可以工作。

An INNER JOIN will not select any row unless the row from BOTH tables exists. 除非两个表中的行都存在,否则INNER JOIN将不会选择任何行。

I believe your problem is that your IDs from the Make table and the BaseVehicle table don't match. 我相信您的问题是Make表和BaseVehicle表中的ID不匹配。

Try running a left join to see which rows are showing up null. 尝试运行左联接以查看哪些行显示为空。

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

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