SQL表连接查询需求:获取分类ID为3的指定商品字段
Solution for Your SQL Query
No worries, let's break this down clearly since you already know we need to join the Categories and Products tables. Here's exactly what you need:
Final SQL Query
SELECT c.CategoryName, p.ProductID, p.ProductName, p.Unit, p.Price FROM Categories c INNER JOIN Products p ON c.CategoryID = p.CategoryID WHERE c.CategoryID = 3;
Let's walk through why this works:
- We use table aliases (
cfor Categories,pfor Products) to make the query cleaner and avoid ambiguity if any field names overlap (though in this case, the fields you're selecting are unique to each table). - The
INNER JOINlinks the two tables usingCategoryID—this is the standard foreign key relationship between a category table and its related products, so even if you don't have the fullProductsschema, this is guaranteed to be the right join condition. - The
WHEREclause filters results to only include rows where the category ID is 3, which matches your requirement.
If by some chance the Products table uses a different name for the category link field (like CatID instead of CategoryID), you'd just adjust the join condition to match, but CategoryID is the standard naming here so this should work right away.
内容的提问来源于stack exchange,提问作者E.M.
相关产品推荐
相关产品推荐

