You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 (c for Categories, p for 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 JOIN links the two tables using CategoryID—this is the standard foreign key relationship between a category table and its related products, so even if you don't have the full Products schema, this is guaranteed to be the right join condition.
  • The WHERE clause 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.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:30:22