简体   繁体   English

在两个桌子上左联接

[英]LEFT JOIN on two tables

I tried the following SQL query to load data from a database to PowerPivot model based on instructions from a PowerPivot manual: 我根据PowerPivot手册中的说明尝试了以下SQL查询以将数据从数据库加载到PowerPivot模型:

SELECT
  Production_Product.Name AS Product,
  Production_ProductCategory.Name AS Category,
  Production_ProductSubcategory.Name AS SubCategory
FROM
  Production_Product
  LEFT OUTER JOIN Production_ProductSubcategory
    ON Production_Product.ProductSubcategoryID = Production_ProductSubcategory.ProductSubcategoryID
  LEFT OUTER JOIN Production_ProductCategory
    ON Production_ProductSubcategory.ProductCategoryID = Production_ProductCategory.ProductCategoryID

Unfortunatelly it keeps throwing a syntax error (missing operator) and I can't find what went wrong. 不幸的是,它不断抛出语法错误(缺少运算符),我找不到发生了什么问题。 It worked perfectly with only one (first) inner join. 它仅需一个(第一个)内部联接即可完美运行。

Since you have not specified the RDBMS I would assume it is MS Access 由于您尚未指定RDBMS,因此我认为它是MS Access

Because the syntax of your query is standard SQL and is correct for all RDBMS but MS access, as you must surround several LEFT JOINs with parenthesis in Access 因为查询的语法是标准SQL,并且对于所有RDBMS(MS访问除外)都是正确的,所以您必须在Access中用括号将多个LEFT JOIN括起来

SELECT
  Production_Product.Name AS Product,
  Production_ProductCategory.Name AS Category,
  Production_ProductSubcategory.Name AS SubCategory
FROM
  ((Production_Product
  LEFT OUTER JOIN Production_ProductSubcategory
    ON Production_Product.ProductSubcategoryID = Production_ProductSubcategory.ProductSubcategoryID)
  LEFT OUTER JOIN Production_ProductCategory
    ON Production_ProductSubcategory.ProductCategoryID = Production_ProductCategory.ProductCategoryID)

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

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