简体   繁体   English

phpmyadmin一列中有多个值

[英]phpmyadmin multiple values in one column

I would like to ask you, does it possible to create a column in phpmyadmin where I could insert (import) more than one value in one column. 我想问你,是否有可能在phpmyadmin中创建一列,我可以在其中插入(导入)多个值。 As example I create a table with name mytable then added column1 (with value 1) and now i would like to add column2(with values 3 5 8). 作为示例,我创建了一个名为mytable的表,然后添加了column1(值1),现在我想添加column2(值3 5 8)。

When I try to do that it only show column2 with value 3 in it... It ignores 5 and 8. 当我尝试执行此操作时,它仅显示其中值为3的column2 ...它会忽略5和8。

You can, but don't 可以,但是不能

Technically a single database field can store multiple values by storing them in a comma-separated string, or a JSON-encoded string. 从技术上讲,单个数据库字段可以通过将多个值存储在逗号分隔的字符串或JSON编码的字符串中来存储多个值。 Many newer versions of MySql also support storing complex objects in a Json Datatype , which can hold just about anything. MySql的许多较新版本还支持将复杂对象存储在Json Datatype中 ,该对象可以容纳几乎所有内容。 However, it is usually a bad idea to do this, and goes against the intent of a relational database. 但是,这样做通常不是一个好主意,并且与关系数据库的意图背道而驰。

Instead consider creating a separate column for each value. 而是考虑为每个值创建一个单独的列。

Better, store optional values in another table. 最好将可选值存储在另一个表中。

Rather than attempting to define a single column to store varying data, create a new table, which references the rows on the first table. 与其尝试定义单个列来存储各种数据,不如创建一个新表,该表引用第一个表上的行。 Below is an example of a Many-to-One data relationship which would solve your problem. 下面是一个多对一数据关系的示例,它将解决您的问题。

table a
-------
id, name, column1
1,  'A',  1
2,  'B',  5

table b
------- 
id, a_id, column2
81, 1,    3
82, 1,    5
83, 1,    8
94, 2,    1
95, 2,    2
96, 2,    3
97, 2,    4
98, 2,    5

By joining these two table, you have a similar result set which is both much more flexible, and sortable/searchable/filterable. 通过将这两个表连接起来,您将得到一个相似的结果集,它既灵活得多,又可排序/可搜索/可过滤。

SELECT a.id, a.name, a.column1, b.column2
FROM a
LEFT JOIN b ON a.id = b.a_id

The answer to " can I do it " is "yes" . 我可以做 ”的答案是“是” However, the answer to " should I do it " is " try to find another way " . 但是,“ 我应该这样做 ”的答案是尝试另辟way径 For example if I have a string like: 例如,如果我有一个类似的字符串:

$mystring = "1 2 3 4 5";

If I execute a query to insert this into a table column field it will insert it. 如果执行查询以将其插入到表列字段中,它将对其进行插入。 If this is the only way you can tackle your algorithm then I suggest you insert data values split with / or - so when you retrieve you can explode them and get their values, but I do not suggest doing it this way. 如果这是解决算法的唯一方法,那么我建议您插入用/-分割的数据值,以便在检索时可以explode它们并获取其值,但我不建议这样做。 Instead, I suggest you to try and find another solution to your problem before you go for this one. 相反,我建议您先尝试解决问题的另一种方法。

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

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