1

这是我的第一篇文章。我知道这是非常低效且重复的代码,实际上不会起作用,我需要做的就是将所有这些输出合并到 1 个 select 语句中。我显然对此很陌生,但是我整天都在做这件事,我只是无法朝着正确的方向开始,每个片段都是独立的......请帮助!

本质上,我正在使用具有许多表的数据库,并且要为每一列获取正确的数据,我经常需要考虑 3 个带有连接的表。

感谢您的任何见解或帮助!

SELECT 
  Product.ProductID,
  (
  SELECT Abbreviation AS Country
  FROM Product
  LEFT JOIN ProductCountry 
    ON Product.ProductID = ProductCountry.ProductID
  LEFT JOIN Location 
    ON ProductCountry.LocationID = Location.LocationID
  GROUP BY Product.ProductID
  ),

  (
  SELECT r.ResourceName AS Manufacturer, rr.ResourceName AS Brand
  FROM Product p
  LEFT JOIN Resource r 
    ON p.ManufactureCode = r.ResourceID
  INNER JOIN Resource rr 
    ON p.BrandCode = rr.ResourceID
  ),

  Product.Name,
  Product.UPC,
  Product.Size,

  (
  SELECT Unit.abbreviation AS Measure
  FROM Product
  LEFT JOIN Unit 
    ON Product.Unit = Unit.UnitID
  ),

  (
  SELECT Category.ParentID AS Category, Category.Description AS Sub_Category
  FROM Product
  LEFT JOIN ProductCategory 
    ON Product.ProductID = ProductCategory.ProductID
  LEFT JOIN Category 
    ON ProductCategory.CategoryID = Category.CategoryID
  ),

  (
  SELECT i.Description AS INGREDIENTS, i.MayContain AS Allergen_Statement
  FROM Product
  LEFT JOIN Ingredient i 
    ON Product.ProductID = i.IngredientID
  ),

  (
  SELECT GROUP_CONCAT( Special.Description SEPARATOR ', ' ) AS Free_From
  FROM Product
  LEFT JOIN ProductSpecial 
    ON Product.ProductID = ProductSpecial.ProductID
  LEFT JOIN Special 
    ON ProductSpecial.SpecialID = Special.SpecialID
  GROUP BY Product.ProductID
  )

FROM Product, ProductStatus
WHERE ProductStatus.ProductStatusID = 1
4

1 回答 1

0

首先,一些注释和假设......

  • 我假设该Country列在Location表中,否则您为什么还要麻烦加入它。

  • 如果这部分有问题,请将第二个连接更改为LEFT JOIN. 我偶尔会遇到麻烦,left join a to b然后是inner join b to c. 我发现继续LEFT JOIN前进更容易,所以我的示例左连接两个表:

      LEFT JOIN Resource r 
        ON p.ManufactureCode = r.ResourceID
      INNER JOIN Resource rr 
        ON p.BrandCode = rr.ResourceID
    
  • 你以Resource两种不同的方式加入。MySQL(以及所有主流数据库)完全可以。您只需为其中一个或两个连接设置别名。我给其中一个起了别名:

    LEFT JOIN Resource ON Product.ManufactureCode ...
    LEFT JOIN Resource BrandResource ON Product.BrandCode...
    
  • 我不知道你的例子ProductStatus是如何加入的。你必须提供那个。

  • 使用下面的示例从小处着手。加入Country, 然后当你得到它的工作时,加入Manufacturer, then Brand, thenMeasure等等。查询没有做很多高级的东西;它很复杂,主要是由于表的数量众多。一次解决一个,你会赢的:)

  • 最后,正如@Bohemian 在评论中指出的那样,GROUP BY无法将其提升到顶部。实际上,它可能可以,但它会使事情变得难以置信。我把它作为一个子查询。

这是最终结果。请注意,它没有经过测试,因为它很大而且我没有表结构或示例数据。但主要是因为它很大:) 无论如何,这只是一个例子。

SELECT 
  Product.ProductID,
  Location.Country,
  Resource.ResourceName AS Manufacturer,
  BrandResource.ResourceName AS Brand,
  Product.Name,
  Product.UPC,
  Product.Size,
  Unit.Abbreviation AS Measure,
  Category.ParentID AS Category,
  Category.Description AS Sub_Category,
  Ingredient.Description AS Ingredients,
  Ingredient.MayContain AS Allergen_Statement,
  (SELECT GROUP_CONCAT( Special.Description SEPARATOR ', ' ) AS Free_From
    FROM Product
    LEFT JOIN ProductSpecial 
      ON Product.ProductID = ProductSpecial.ProductID
    LEFT JOIN Special 
      ON ProductSpecial.SpecialID = Special.SpecialID
    GROUP BY Product.ProductID
    )
FROM Product
  INNER JOIN ProductStatus ON ... however it's joined
  LEFT JOIN ProductCountry ON Product.ProductID = ProductCountry.ProductID
  LEFT JOIN Location ON ProductCountry.LocationID = Location.LocationID
  LEFT JOIN Resource ON Product.ManufactureCode = Resource.ResourceID
  LEFT JOIN Resource BrandResource ON Product.BrandCode = BrandResource.ResourceID
  LEFT JOIN Unit ON Product.Unit = Unit.UnitID
  LEFT JOIN ProductCategory ON Product.ProductID = ProductCategory.ProductID
  LEFT JOIN Category ON ProductCategory.CategoryID = Category.CategoryID
  LEFT JOIN Ingredient ON Product.ProductID = i.IngredientID
WHERE ProductStatus.ProductStatusID = 1
于 2013-04-20T04:41:07.717 回答