多个值存储在MySQL数据库的单列中?
如何在一个属性中存储多个值。 例如: 栏目名称:电话 我想将 5,7 和 16 这三个不同的值存储在一行的一列(电话)中。
How can i store multiple values in one attribute.
for example:
column name: telephone
i want to store 5,7 and 16 this three different values in one column (telephone) in one row.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不应该这样做。这说明数据库设计不合理。
但如果您必须在不改变数据库设计的情况下做到这一点。您可以使用特殊的胶水将所有电话号码连接起来,以确保该胶水不会成为任何电话号码的一部分,例如
%
。因此,5、7 和 16 将全部存储在 varchar 类型的一列中,作为5%7%16
,稍后您的应用程序会根据需要将它们拆分。You should not be doing that. It goes to show that the DB has not been designed properly.
But if you have to do it without altering your DB design. You can join all the telephone numbers with a special glue, which is ensured not to be part of any telephone number, something like
%
. So 5, 7 and 16 will all be stored in one column of type varchar as5%7%16
, later your application and split them up as needed.您可以用“,”或“;”分隔不同的值吗?
当您查询内容时,只需在分隔符上拆分字符串...
否则我认为这是不可能的。
You could separate the different values by a "," or ";"?
When you query the content, just split the string on the delimiter....
Otherwise I don't think this is possible.
例如,用逗号分隔它们?请注意,不建议这样做,如果同一列需要多个值,需要多行,或者最好有一个辅助表来保存所有电话号码。
但是,如果该列是 VARCHAR 或其他字符串形式,则可以执行
INSERT INTO table SET Phone = '5,7,16'
。将值拆分回单独的条目比较困难,通常必须在程序代码中执行此操作。By separating them with commas, for example? Be advised that this is not recommended, if you need multiple values for the same column, you need multiple rows, or preferably an auxiliary table to hold all phone numbers.
But you can just do
INSERT INTO table SET telephone = '5,7,16'
if that column is VARCHAR or some other string form. Splitting the values back to separate entries is harder and you usually have to do that in your program code.