Jump to content

Beginner question: Inserting in a single row with multiple data in a column


bugzy

Recommended Posts

Hello guys. This is just a beginner question and I need some clarification regarding this.

 

I have this tables with their columns

 

Category Table

Category_ID (PK) | Category_Name | Category_Position | Category_Visibility

 

Item Table

 

Item_ID (PK) | Category_ID (FK) | Item_Name | Item_Description | Item_Price | Item_Stocks | Item_Sale | Date_Added

 

 

 

What I want is, an item can have multiple categories so I wonder putting category_id as the FK in the item table is right? or is it the other way around?

 

I also wonder how would I able to insert an item with a multiple categories on it? Will I need to put a loop on it?

 

 

 

 

Anyone?

 

Link to comment
Share on other sites

usually you would have 3 tables for this:

 

categories:

Category_ID (PK) | Category_Name | Category_Position | Category_Visibility

 

cat_items

Category_ID | Item_ID

 

items:

Item_ID (PK) | Item_Name | Item_Description | Item_Price | Item_Stocks | Item_Sale | Date_Added

 

you would then put your Category/Item pairs in the two column table.

 

then do a query like so:

 

select * from cat_items ci 
left join categories c using(Category_ID)
left join items i using(Item_ID)
where ci.Category_ID = 123

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.