I'm pretty new to SQL and I am currently trying to make a simple product page in PHP and MySql. Now I have a products
table which has my product id
, name
, price
and stock
.
Edit: Sample products
tables:
|----------------------------------------------------------------|
| id | name | price | stock |
|----------------------------------------------------------------|
| 1 | Guess T-Shirt | 10 | 100 |
|----------------------------------------------------------------|
The thing I'm trying to do is having different categories for the products, such that each product can fall into multiple categories.
Here are the options that each product will have:
Now as you see, there are different categories each with sub-categories.
On first thought, I wanted to have a table with all these categories as rows and a foreign key to id
from my products
table. I realized it would not be the best way to do it, but I also don't know what would be the right way to do it.
I apologize in advance if the question might be irrelevant. I don't have any codes to show, I just need some input on the correct database structure.
If you require more detail, please comment down and I'll update this post. Thanks
Table basics. SQL Server tables are contained within database object containers that are called Schemas. The schema also works as a security boundary, where you can limit database user permissions to be on a specific schema level only. You can imagine the schema as a folder that contains a list of files.
You need a product
table with this fields
primary key
Then a table category
for the left side of the menu
primary key
and a pivot table product_category
Finally your query will be
SELECT p.*
FROM products p
INNER JOIN product_category pc
ON p.product_id = pc.product_id
WHERE
p.Visibility = @visibilty
AND p.Stock = @Stock
AND p.Discount = @Discount
AND p.Remarks = @Remarks
AND pc.Category_id in (@category);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With