Jump to content

How to insert multiple id in one row?


Bravat

Recommended Posts

I have table related where i need to store all items that are related to the specific one. I know how to create multiple rows (for example item:id1 is related to item:id2, item:id1 is related to item:id3, item:id1 is related to item:id4 and so one ). How can store all that in one row (item:id1 is related to item:id2,item:id3....) and later be able to use this for query?

Link to comment
Share on other sites

I think best would be to create two tables and relations between them. Something like this.

 

items

----------

item_id

item_name

 

 

related_items

----------------

item_id

item_name

related_id (references to the table items and field item_id)

 

 

So item in table items can have multiple child items in table related_items. Then you can easily get all the related items for certain item with using a JOIN in your query. Not sure thought if I understood correctly what you are after.

 

Link to comment
Share on other sites

That is the way I made table. I want to put more then one item_id in single row, and latter to be able to use stored item_ids for query. For example:

item_id:1 is related with item_id2, item_id3, item_id4... (I don't know how to do this).

Latter I want to do something like this:

SELECT * FROM item WHERE item_id = (and here I insert values for related_id)

Link to comment
Share on other sites

I am standing confused  :confused:

I don't want to have situation where I have a 100 rows for one item (id1 is related to id2(one row), id2-id3 (second row) and so on). In that case i will have one big table (for example if I have 1000 items, that are related with 100 other items that will be 100,000 rows). I want to put 100 related items in  one row, and that is puzzling me.

Link to comment
Share on other sites

100 000 rows is not much in a table if the database and queries are built in right way and optimized. Other choice would be that you have a relations table that links the products together with their id.

 

items

-----------

id

item

 

 

relations

-----------

item_id (references to items (id) )

related_id (references also to items (id) )

 

This way you have only one table with items, and every item appears only one time. The other table is just connecting the relations between these items.

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.