分隔字段的逗号分隔值并将它们添加到临时表的整数字段中
我有一个表 member_details ,其中包含字段“preferred_location”(varchar),该表具有逗号分隔的值,例如来自列表框选择的“19,20,22”......
现在我还有另一个表 city_master ,其字段为“city_id”(int )和“city_name”(varchar)...
现在我想分离“preferred_location”(varchar)值并将它们添加到临时表的整数字段中,这样我就可以在临时表的 city_id(int) 和city_master 的 city_id(int) ,然后可以从 city_master 的 city_name 获取城市名称...
这就是我在 MySQL 中需要的所有东西 - 存储过程或函数。我将它与 c#.net 一起使用。
I have one table member_details with field "preferred_location" (varchar) that has comma separated values like "19,20,22" that come from a listbox selection ....
Now I also have another table city_master having field "city_id" (int) and "city_name" (varchar)...
Now I want to separate "preferred_location" (varchar) values and to add them in integer field of temp table so I can make an inner join between city_id(int) of the temp table and city_id(int) of city_master and then can get city name from city_name of city_master...
This is all stuff I need in MySQL - either a stored procedure or a function. I am using it with c#.net.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
坦率地说,这听起来是一个糟糕的设计。如果您需要单独的整数值,请相应地修改数据库结构,并首先单独保存这些值。
我的意思是,您知道它会导致什么结果 - 因为您将值作为列表存储在字符串中,所以您已经将自己调整到每次想要连接表时都需要展开值的位置。
这就像把马放在马车后面一样。
Frankly, this sounds like a bad design. If you need the integers values separately, then modify your database structure accordingly, and save the values separately to begin with.
I mean, you see where it leads to - because you stored the values as a list in a string, you have maneuvered yourself into a position where you need to unwind the values each time you want to join the tables.
That's like putting the horse behind the wagon.
如果这些整数很小,例如 19、20、22 等,则只需使用较小的 16 或 8 位整数(如数据库支持的),并且它不应该比字符串占用更多的空间(可能甚至更少)。
If these integers are small, like 19,20,22 etc just use smaller 16 or 8 bit integers (as supported by your database) and it should not take much more space than a string (possibly even less).
制作了一些模拟示例,但这也应该适用于 LinqToMySql。
Made up some mock up example, but this should work with LinqToMySql as well.
感谢您的建议,但就我而言,最好将首选位置城市的 ID 存储为逗号分隔。
我有一个创建临时表的过程,然后我可以使用
city_master
表的内部联接来获取城市名称。thanks for your suggestion but in my case it is better to store ids of preferred location cities as comma separated.
I have a procedure that makes a temporary table and then I can use inner join with
city_master
table to get city names.