这是我的第一篇文章。我知道这是非常低效且重复的代码,实际上不会起作用,我需要做的就是将所有这些输出合并到 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