How to get all product in a father_id in php mysql
Get the solution ↓↓↓Solution:
Try this:
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'
Answer
Solution:
Do you mean something like
SELECT
childbrand,name AS child_brand_name
product.name AS product_name
FROM brand AS childbrand
INNER JOIN brand AS parentbrand ON parentbrand.id=childbrand.parent_id
INNER JOIN product_brand ON product_brand.brand_id=childbrand.id
INNER JOIN product ON product_brand.product_id=product.id
WHERE parentbrand.name='Apple';
Answer
Solution:
If you want to get the product name specifically of Apple and Samsung which is not stored at product_brand (see that from the sample data). You could fix the query as below:
SELECT *,
CASE b.name WHEN 'Apple' THEN 'iOS'
WHEN 'Samsung' THEN 'Android' ELSE '-' END
FROM
brand b
Edited 2: If you want to get all the product name based on the parent_id. Could try the below:
SELECT *,
CASE b.name WHEN 'Apple' THEN 'iOS'
WHEN 'Samsung' THEN 'Android' ELSE '-' END as type_of_os
FROM
brand b
WHERE parent_id=0
UNION
SELECT
b.*,
CASE b2.name WHEN 'Apple' THEN 'iOS'
WHEN 'Samsung' THEN 'Android' ELSE '-' END as type_of_os
FROM
brand b
INNER JOIN
brand b2 ON b.parent_id = b2.id
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: warning: a non-numeric value encountered in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.