我想从2个表中显示类别,子类别,产品和价格

时间:2015-08-12 17:02:11

标签: mysql

我有2tables.one是包含id,cat_name,parent_id的类别。此表包含类别和子类别。类别的parent_id为0,子类别的父ID为类别的id。另一个表是产品包含id,pnname,price,pro_id.pro_id是子类别的id。

未给出任何此查询的正确值

  id cat_name parent_id      id  pname  pro_id
  --------------------      --------------------
   1 Electronics  0           1  abc     3
   2 Clothing     0           2  def     4
   2 Clothing     0
   3 Phone        1
   4 Kurti        2

  output:
  category      subcategory    Product 
  --------------------------------------
   Electronics     Phone       abc  
   Clothing        Kurti       def
  select product.pname from product where product.pro_id=(SELECT c1.cat_name, c2.cat_name FROM category c1, category c2 WHERE c1.id = c2.parent_id)

SELECT product.pname, category.cat_name
FROM product
INNER JOIN category ON category.id = product.pro_id
OR category.parent_id =0
 SELECT product.pname, category.cat_name
 FROM product INNER JOIN category ON product.pro_id=(SELECTc1.cat_name,  c2.cat_name FROM category c1, category c2 WHERE c1.id = c2.parent_id)

1 个答案:

答案 0 :(得分:1)

SELECT * FROM category c1
LEFT JOIN category c2
    ON c2.parent_id = c1.id
LEFT JOIN product p
    ON p.pro_id = c2.id;