multiple value stored in single coloumn in MySQL database?

◇◆丶佛笑我妖孽 提交于 2019-12-11 10:28:08

问题


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.


回答1:


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 as 5%7%16, later your application and split them up as needed.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1973905/multiple-value-stored-in-single-coloumn-in-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!