分隔字段的逗号分隔值并将它们添加到临时表的整数字段中

发布于 2024-08-16 05:09:44 字数 389 浏览 6 评论 0原文

我有一个表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦中的蝴蝶 2024-08-23 05:09:44

坦率地说,这听起来是一个糟糕的设计。如果您需要单独的整数值,请相应地修改数据库结构,并首先单独保存这些值。

我的意思是,您知道它会导致什么结果 - 因为您将值作为列表存储在字符串中,所以您已经将自己调整到每次想要连接表时都需要展开值的位置

这就像把马放在马车后面一样。

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.

旧伤慢歌 2024-08-23 05:09:44

如果这些整数很小,例如 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).

苍景流年 2024-08-23 05:09:44

制作了一些模拟示例,但这也应该适用于 LinqToMySql。

class user {
public string name {get;set;}
public int id {get;set;}
}
class member_detail {
public int user_id {get;set;}
public string prefered {get;set;}
}

class city_master{
public int code {get;set;}
public string name {get;set;}
}
void Main()
{
var users = new List<user>();
users.Add(new user(){name = "Mary",id = 1});
users.Add(new user(){name = "John",id=2});
    var details = new List<member_detail>() ;
    details.Add(new member_detail(){user_id=1,prefered="1,2,3"});
    details.Add(new member_detail(){user_id=2,prefered="3,5"});
    var cities = new List<city_master>();

    cities.Add(new city_master(){code =1,name="Moscow"});
    cities.Add(new city_master(){code =2,name="London"});
    cities.Add(new city_master(){code =3,name="Paris"});
    cities.Add(new city_master(){code =4,name="Rome"});
    cities.Add(new city_master(){code =5,name="Madrid"});

    users.Select(u=>new {u.name,cities=
    details.Where(d=>d.user_id==u.id)
    .SelectMany(d=>d.prefered.Split(','))
    .Join(cities,c=>c,d=>d.code.ToString(),(a,b)=>new {b.name})}).Dump();

}

Made up some mock up example, but this should work with LinqToMySql as well.

class user {
public string name {get;set;}
public int id {get;set;}
}
class member_detail {
public int user_id {get;set;}
public string prefered {get;set;}
}

class city_master{
public int code {get;set;}
public string name {get;set;}
}
void Main()
{
var users = new List<user>();
users.Add(new user(){name = "Mary",id = 1});
users.Add(new user(){name = "John",id=2});
    var details = new List<member_detail>() ;
    details.Add(new member_detail(){user_id=1,prefered="1,2,3"});
    details.Add(new member_detail(){user_id=2,prefered="3,5"});
    var cities = new List<city_master>();

    cities.Add(new city_master(){code =1,name="Moscow"});
    cities.Add(new city_master(){code =2,name="London"});
    cities.Add(new city_master(){code =3,name="Paris"});
    cities.Add(new city_master(){code =4,name="Rome"});
    cities.Add(new city_master(){code =5,name="Madrid"});

    users.Select(u=>new {u.name,cities=
    details.Where(d=>d.user_id==u.id)
    .SelectMany(d=>d.prefered.Split(','))
    .Join(cities,c=>c,d=>d.code.ToString(),(a,b)=>new {b.name})}).Dump();

}
挽清梦 2024-08-23 05:09:44

感谢您的建议,但就我而言,最好将首选位置城市的 ID 存储为逗号分隔。

我有一个创建临时表的过程,然后我可以使用 city_master 表的内部联接来获取城市名称。

Create Procedure parseAndStoreList(in thingId int, in i_list varchar (128), 
                                   out returnCode smallInt) 
BEGIN 
         DECLARE v_loopIndex default 0;
         DECLARE Exit Handler for SQLEXCEPTION 
BEGIN
        call saveAndLog(thingId, 'got exception parsing list');
        set returnCode = -1;
END;

call dolog(concat_ws('got list:', i_list)); 
          pase_loop: LOOP set v_loopIndex = v_loopIndex + 1; 
call dolog(concat_wc(',', 'at loop iteration ', v_loopIndex);
          LOOOP parse_loop; 
set returnCode = 0;
END; 

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.

Create Procedure parseAndStoreList(in thingId int, in i_list varchar (128), 
                                   out returnCode smallInt) 
BEGIN 
         DECLARE v_loopIndex default 0;
         DECLARE Exit Handler for SQLEXCEPTION 
BEGIN
        call saveAndLog(thingId, 'got exception parsing list');
        set returnCode = -1;
END;

call dolog(concat_ws('got list:', i_list)); 
          pase_loop: LOOP set v_loopIndex = v_loopIndex + 1; 
call dolog(concat_wc(',', 'at loop iteration ', v_loopIndex);
          LOOOP parse_loop; 
set returnCode = 0;
END; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文