SQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
harshitpunn
Forum Newbie
Posts: 7
Joined: Tue Jul 01, 2014 1:03 pm

SQL

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SQL

Post 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
harshitpunn
Forum Newbie
Posts: 7
Joined: Tue Jul 01, 2014 1:03 pm

Re: SQL

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SQL

Post 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.
harshitpunn
Forum Newbie
Posts: 7
Joined: Tue Jul 01, 2014 1:03 pm

Re: SQL

Post by harshitpunn »

I am talking about database only. I am using Mysql
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SQL

Post by requinix »

Then I can't figure out what you're asking. How about giving an explanation that's longer than one sentence?
harshitpunn
Forum Newbie
Posts: 7
Joined: Tue Jul 01, 2014 1:03 pm

Re: SQL

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: SQL

Post 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.
Post Reply