brand(id, parent_id, name)
      1 |     0    | Apple
      2 |     0    | Samsung
      3 |     1    | Iphone 3S
      4 |     1    | Iphone 4S
product(id, name)
        1 | OS
        2 | Andriod
product_brand(product_id, brand_id)
                  1     |    3
                  1     |    4

和mysql:
如何在product name时获取brand name = Apple
SELECT * FROM product_brand
LEFT JOIN brand ON brand.id = product_brand.brand_id
WHERE brand.id = 1 // Apple

例:如果get Apple(id=1)是result=>OS,如果get Samsung(id=2)是result=>Andriod

最佳答案

试试这个:

SELECT
  p.name
FORM product p
LEFT JOIN product_brand pb ON p.id = pb.product_id
LEFT JOIN brand b ON pb.brand_id = b.id
LEFT JOIN brand b1 ON b.id = b1.parent_id
WHERE b1.name = 'Apple'

关于php - 如何在PHP MySQL中的father_id中获取所有产品,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11218433/

10-12 12:41
查看更多