我坚持查询
我有这个表PRODUCT,其中我列出了产品ID,标题,子类别ID和类别ID的产品。还有另一个表(procat),其中我有procat id和父id,procat title。所以基本上一个具有父ID的id是一个子类别,如果为0,那么它的类别就是我的逻辑
现在基本上我想要做的是获取产品标题,类别标题和子类别标题我可以获得类别但是我怎样才能获得子类别
SELECT
product.id,
product.title AS product_title,
procat.title AS category_title
FROM product,
procat
WHERE product.procatid = procat.id
OR product.procatsubid = procat.id
AND product.procatid = '31'
AND product.procatsubid = '21'
我也尝试了左连接,但无济于事
答案 0 :(得分:0)
修改 - 感谢您的澄清。似乎产品外键到类别可能存在冗余(即产品应该只能通过procat SubCategory进行分类,并且通过导航procat.parent(可能递归)来获得父类别ID
SELECT
product.id,
product.title AS product_title,
cat.title AS category_title,
subcat.title AS subcategory_title
FROM product
INNER JOIN procat cat on product.procatid = cat.id
INNER JOIN procat subcat on product.procatsubid = subcat.id
procat
WHERE product.procatid = '31'
AND product.procatsubid = '21'
-- The below should be redundant, but I guess you can use them to ensure that the product FKs are correct and the table integrity is maintained
AND cat.parentid = 0
AND subcat.parentid <> 0