Query Mysql Product Category Subcategory

0

I have this relationship:

And I would like to make the appropriate consultation that the Products that belong only to a Subcategory, of a specific category, would throw at me.

If you could help me, I'd appreciate it, I'm going around with it, but even the JOINs are a bit of a nuisance to me and I have not yet managed to make it functional.

I am open to suggestions, if the relationship is not appropriate, even so, and of course I will be taking the suggestion, I would like if the Query were feasible that would throw me the result as it is at this moment.

Thanks in advance .-

    
asked by J'Esaa 27.10.2018 в 02:34
source

1 answer

2

This consultation will bring you all the products with their respective categories, subcategories, etc. Keep in mind that: The JOIN (join, combine) SQL statement allows you to combine records from one or more tables in a relational database.

select p.id, p.nombre_prod, c.nombre_cat, s.nombre_subCat from producto p join prod_cat_subcat pcs on pcs.id_prod = p.id join categoria c on c.id = pcs.id_cat join subcategoria s on s.id = pcs.id_subcat where s.id = 1;

If you are only interested in the data, regardless of whether they are loaded in the other tables, you can use left join: The result of this operation always contains all the records in the table on the left (Products), regardless of whether there is a corresponding record in the table on the right (Category, subcategory, etc.).

select p.id, p.nombre_prod, c.nombre_cat, s.nombre_subCat from producto p left join prod_cat_subcat pcs on pcs.id_prod = p.id left join categoria c on c.id = pcs.id_cat left join subcategoria s on s.id = pcs.id_subcat where s.id = 1;

    
answered by 27.10.2018 / 03:00
source