2012-05-01 41 views
0

我在这里坚持一个查询获取产品的ID,然后从另一个表

我有这个表产品在那里我有产品编号,名称,类别ID和类别ID上市产品获得子类别和产品称号。还有另一个表(procat),我有procat id和父母id,procat标题。所以基本上一个具有父母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

编辑 - 感谢澄清。看起来产品外键可能存在冗余(即产品可能只是由procat子类别分类,并且父类别id是通过导航procat.parent(可能递归地)得到的

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 
+0

获得多个价值与这一个....我需要得到的是产品标题,我有类别id所以cataegory标题和我有子类别ID所以子类别的标题实际上在procat的类别和子类别列出相同的方式只有区别如果procat id具有父级id,则它们是父级ID,这意味着它的子类别为0 –

相关问题