user3025122
user3025122

Reputation: 17

Insert multiple values in single cell in MySQL

I have searched different pages. Is it possible to insert many values in single cell

enter image description here

Under id 475 can all values be stored like related_id=281,283,284,285,286

INSERT INTO LOGI (related_id)VALUES(281), (283), (284), (285), (286)

UPDATE1 Now if I want to update the all logi_keyword_id for logiid=613Update issue UPDATE logi_logi_keyword SET logi_keyword_id='102' WHERE EXISTS logi_id='543' but it gives error- #1062 - Duplicate entry '543-102' for key 'PRIMARY'

Upvotes: 0

Views: 5685

Answers (2)

jszobody
jszobody

Reputation: 28959

Sure if you really want to, assuming related_id is a varchar or text column type...

INSERT INTO LOGI (related_id) VALUES('281,283,284,285,286');

However this breaks the whole foreign key paradigm. You won't be able to run SELECT queries and join tables based on this column.

Better to create a cross-reference table. Call it LOGI_RELATED perhaps, with logi_id and related_id columns. Then you can have one LOGI record with relationships to multiple RELATED records.

Sounds like you may want to do some research on "many to many relationships" and improve your database design.

Upvotes: 3

KB9
KB9

Reputation: 609

for this situation you need another table to establish a one to many relation using your table id as a foreign key.
something like this:

Another table

id | your_table_id | related_id

1 | 475 | value

2 | 475 | another_value

Upvotes: 0

Related Questions