0

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'

Community
  • 1
  • 1
user3025122
  • 17
  • 2
  • 9

2 Answers2

3

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.

jszobody
  • 28,495
  • 6
  • 61
  • 72
  • @Hanky웃Panky I see OP asking to "insert many values in single cell." All "under id 475." That doesn't sound like multiple rows to me. – jszobody Jan 16 '14 at 15:01
  • But taking a look at the problem image link they gave, you'll notice its the other way. They have a new row for every value – Hanky Panky Jan 16 '14 at 15:02
  • @Hanky웃Panky That image is the problem, not how he wants it. He wants all these values "under id 475" which is a single row. – jszobody Jan 16 '14 at 15:03
  • Fair enough, that makes sense. – Hanky Panky Jan 16 '14 at 15:04
  • @jszobody you can store like this and use the explode and implode functions to get the data and merge it, but its preferable to store them in a different table with uid and related_id as two columns with foreign key on uid – tHeSiD Jan 16 '14 at 15:07
  • @tHeSiD explode/implode work great in PHP, not so much for SQL joins – jszobody Jan 16 '14 at 15:08
  • @jszobody I was talking about php, since you look like you are on php/mysql. You can store the concatenated string of ids in the field and explode it using php whenever you want, and store it again into the db with implode – tHeSiD Jan 16 '14 at 15:12
  • @all Thanks to every body, but new table cannot be created so I have to use this table only. – user3025122 Jan 16 '14 at 15:40
0

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

KB9
  • 599
  • 2
  • 7
  • 16