简体   繁体   English

在一个mysql列中存储键值列表的最佳方法是什么

[英]What is the best way to store a list of key values in one mysql column

I need to store data that can be variable, eg (data1='test1',data2='test2'). 我需要存储可以是可变的数据,例如(data1 ='test1',data2 ='test2')。

Is json the best way to do this ? json是执行此操作的最佳方法吗? or there is a better way? 还是有更好的方法?

Creating a table to contain the value, can't be done since the keys are dynamic. 由于键是动态的,因此无法创建包含值的表。

create a table where columns would be your keys and rows would contain values 创建一个表,其中列将作为您的键,行将包含值

And never use your database to store data like json. 永远不要使用您的数据库来存储json之类的数据。 that's disgusting 真恶心

I would do it in JSON, yes. 我会用JSON完成,是的。 Just be sure to have a column data size that's long enough to accomodate your JSON strings. 只要确保列数据的大小足以容纳您的JSON字符串即可。

You might want to have a look at MongoDB and other non-structured databases one day though, they're the more elegant way to do that. 不过,您可能有一天可能希望了解一下MongoDB和其他MongoDB数据库,它们是一种更为优雅的方法。

JSON is a format for storing transfering values into/within javascript. JSON是一种用于在javascript中/其中存储传输值的格式。

MySQL is a relational database and you should structure your data accordingly - ie it should be normalised. MySQL是一个关系数据库,您应该相应地构造数据-即应该对其进行规范化。 From the information you've provided it is impossible to say whether the right relational model for your data would across multiple columns or across multiple rows . 从您提供的信息来看,无法确定是数据的正确关系模型是跨多列还是跨多行

You can use serialize() to store your array in a single column. 您可以使用serialize()将数组存储在单列中。 Use unserialize() when fetching the data from your table. 从表中获取数据时,请使用unserialize()

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

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