Page 1 of 1
SQL
Posted: Tue Jul 01, 2014 1:07 pm
by harshitpunn
How to add multiple categories(same table) to a product
category_id category_title
1 c1
2 c2
product_id product_title category_id
1 c1 1,2
2 c2 2
Re: SQL
Posted: Tue Jul 01, 2014 3:03 pm
by requinix
Make a third table of just product_id and category_id, then put a row in it for each pairing.
Code: Select all
product_id | category_id
-----------+------------
1 | 1
1 | 2
2 | 2
Re: SQL
Posted: Tue Jul 01, 2014 3:24 pm
by harshitpunn
The admin will be posting products from backend with multiple categories i wnat to know how to take multiple categories at once dynamically and how to map them
Re: SQL
Posted: Tue Jul 01, 2014 3:29 pm
by requinix
So you're not asking a database design question, then?
How you do it depends on the software. Which we know nothing about because you haven't told us what it is or how it works or really anything useful like that.
Re: SQL
Posted: Tue Jul 01, 2014 3:43 pm
by harshitpunn
I am talking about database only. I am using Mysql
Re: SQL
Posted: Tue Jul 01, 2014 4:08 pm
by requinix
Then I can't figure out what you're asking. How about giving an explanation that's longer than one sentence?
Re: SQL
Posted: Wed Jul 02, 2014 12:48 am
by harshitpunn
I am creating a eCommerce and i am trying to add more than 1 category to a single product . I am using PHP for that. To tell u clearly i am having 1 table having name categories and another table products. The categories table have category_id and category _title. The product table has product_id,product_title and category_id. I want to add multiple category_ids to a single product
Re: SQL
Posted: Wed Jul 02, 2014 6:14 am
by Celauran
As requinix already stated in his first post, you want to use a join table for that. product_id and category_id as your columns, and one row per product per category.
Code: Select all
product_id | category_id
-----------+------------
1 | 1
1 | 2
2 | 2
The category_id column in your products table would only be useful if any given product could only belong to one category. It's not useful here.