1

我有 2 张桌子:

1 Category
- cID
- Name
- Active

2 Products
- pID
- Name
- category_id
- active

这就是我想要的:我想要一个来自只有 ACTIVE 产品的类别的列表。

提前致谢

解决方案:SELECT DISTINCT category.* FROM category INNER JOIN products ON category.id = products.c_id WHERE products.active = 0 ORDER BY category.id DESC

4

3 回答 3

3

假设Products.Active = 1意味着活动状态。

SELECT c.*
FROM Category c
INNER JOIN Products p ON c.CID = p.category_id
GROUP BY c.CID
HAVING COUNT(*) = SUM(IF(p.Active = 1, 1, 0)) 
于 2012-10-14T12:44:04.003 回答
1
SELECT * FROM Products
Join Category on Category.ciD = Products.category_id
WHERE Category.Active = 1 AND Products.Active =1

这是假设您的活动列只是 1 或 0。否则根据您存储在那里的值进行调整。

于 2012-10-14T12:27:54.683 回答
1

我建议像这样构建
恕我直言,它的语法显示了您真正想看到的内容-所有具有有效产品的类别

select C.cID, C.Name, C.Active
from Category as C
where C.cID in (select distinct T.category_id from Products as T where T.Active = 1)
于 2012-10-14T17:05:12.480 回答