如何在我的大联接Mysql查询中使用where子句来限制结果?

时间:2018-11-25 01:25:39

标签: mysql

我已经在下面成功编写了此联接查询,以在一个查询中从数据库的每个单独表中获取我需要的所有数据。不幸的是,它给了我来自这些单独表的所有数据。如果e.Design_ID = 1,我仅需要与e.Design_ID对应的数据,但是我在此代码块中尝试了一些“ where e.Design_ID = 1”,但未成功限制查询。我被卡住了。我在想可能需要重组整个查询。我的逻辑可能不对...

非常感谢您的帮助。

SELECT 
a.SKU,
a.Apparel_ID,
a.Apparel_Color_Abbr,
a.Design_ID,
a.City_ID,
a.Category_ID,
a.SEO_Keyword,
a.OC_Product_ID,
b.Apparel_Color_Palette,
b.Apparel_Name,
b.Apparel_Name_Abbr,
b.Apparel_Type,
b.Google_Feed_Cat,
b.Gender,
b.Age_Group,
b.Retail_Price,
b.Apparel_Description,
c.Apparel_Color,
d.OC_Cat_City_ID,
d.OC_Cat_State_ID,
d.City,
d.State,
e.Design_Name,
e.Design_Description
FROM Complete_City_Products AS a
INNER JOIN Apparel AS b ON a.Apparel_ID = b.Apparel_ID
INNER JOIN Apparel_Colors AS c ON b.Apparel_Color_Palette = c.Apparel_Color_Palette AND a.Apparel_Color_Abbr = c.Apparel_Color_Abbr
INNER JOIN Cities AS d ON a.City_ID = d.City_ID
INNER JOIN Designs AS e ON a.Design_ID = e.Design_ID
";

1 个答案:

答案 0 :(得分:1)

SELECT 
a.SKU,
a.Apparel_ID,
a.Apparel_Color_Abbr,
a.Design_ID,
a.City_ID,
a.Category_ID,
a.SEO_Keyword,
a.OC_Product_ID,
b.Apparel_Color_Palette,
b.Apparel_Name,
b.Apparel_Name_Abbr,
b.Apparel_Type,
b.Google_Feed_Cat,
b.Gender,
b.Age_Group,
b.Retail_Price,
b.Apparel_Description,
c.Apparel_Color,
d.OC_Cat_City_ID,
d.OC_Cat_State_ID,
d.City,
d.State,
e.Design_Name,
e.Design_Description
FROM Complete_City_Products AS a
INNER JOIN Apparel AS b ON a.Apparel_ID = b.Apparel_ID
INNER JOIN Apparel_Colors AS c ON b.Apparel_Color_Palette = c.Apparel_Color_Palette AND a.Apparel_Color_Abbr = c.Apparel_Color_Abbr
INNER JOIN Cities AS d ON a.City_ID = d.City_ID
INNER JOIN Designs AS e ON a.Design_ID = e.Design_ID

WHERE e.Design_ID = 1
";

如果放在最后,它应该可以工作。还要检查拼写是否正确。