简体   繁体   English

在MySQL中的单个单元格中插入多个值

[英]Insert multiple values in single cell in MySQL

I have searched different pages . 我搜索了不同的页面 Is it possible to insert many values in single cell 是否可以在单个单元格中插入许多值

在此处输入图片说明

Under id 475 can all values be stored like related_id=281,283,284,285,286 在id 475下可以存储所有值,例如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=613 UPDATE1现在,如果我想将所有logi_keyword_id更新为logiid = 613 更新问题 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' UPDATE logi_logi_keyword SET logi_keyword_id ='102'WHERE EXISTS logi_id ='543'但它给出了错误-#1062-键'PRIMARY'的条目'543-102'重复

Sure if you really want to, assuming related_id is a varchar or text column type... 可以肯定的是,假设related_idvarchartext列类型...

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. 您将无法运行SELECT查询并基于此列连接表。

Better to create a cross-reference table. 最好创建一个交叉引用表。 Call it LOGI_RELATED perhaps, with logi_id and related_id columns. 说它LOGI_RELATED也许,与logi_idrelated_id列。 Then you can have one LOGI record with relationships to multiple RELATED records. 然后,您可以拥有一个LOGI记录,并具有与多个RELATED记录的关系。

Sounds like you may want to do some research on "many to many relationships" and improve your database design. 听起来您可能想对“许多关系”进行一些研究,并改善数据库设计。

for this situation you need another table to establish a one to many relation using your table id as a foreign key. 对于这种情况,您需要另一个表使用表ID作为外键建立一对多关系。
something like this: 像这样的东西:

Another table 另一张桌子

id | id | your_table_id | your_table_id | related_id related_id

1 | 1 | 475 | 475 | value

2 | 2 | 475 | 475 | another_value another_value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM