向 mysql 表添加不同的值类型列
有没有办法在 MySQL 表的同一列中添加不同类型的值?
编辑:每一行的列中都有一个 varchar 数据字段,该字段在另一列的另一个字段中具有与其关联的数值。是否可以将这些数字在某些行中保存为 float 类型,而在其他行中保存为 int 类型?
Is there any way to add different types of values in the SAME column in a MySQL table?
EDIT: Every row has a varchar data field in a column that has a numerical value associated to it in another field in another column. Is it possible to save these numbers as float type in some rows and int in some others?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它需要 3 列:
然后您可以检查 IsInt 字段以查看您使用的数据类型。
It takes 3 columns:
Then you can check the IsInt field to see which data type you used.
如果通过类型来表示数据的值不是由列所指定的数据类型所表示的,例如 blob 字段中的整数或类似的值,那么就不是。
数据可以转换为序列化格式(例如 JSON 或 XML)并保存在文本字段中,但出于多种原因,这对于大多数应用程序来说是不可取的,并且像 MongoDB 或 Redis 这样的键值存储可能会更好如果这是所期望的,则此行为。
有人想知道你为什么要这样做?您能否指定您希望通过此完成什么,也许我们可以找到更好的方法。
If by types, you mean data with values represented by something other than what the column has for its specified datatype, as in ints in blob fields or some such, then no.
Data could be turned into a serialized format such as JSON or XML and saved in a text field, but this isn't advisable for most applications for a whole host of reasons, and a key value store like MongoDB or Redis would probably much better approximate this behavior if that's what's desired.
One wonders why you would want to do this? Can you specify what you'd be looking to accomplish by this and perhaps we can find a better method.
我不确定 MySQL 对对象的支持,但这可能是一个解决方案。
无论如何,如果您只需要管理一种信息,但不需要类型,则可以将其存储为“字符串”,并在代码上做好解析它的工作。
I'm not sure about MySQL support for objects, but it could be a solution.
Anyway, if you need manage only one information, but you don't need type, you can store it as "string" and do a really good job on code to parse it.