0

我以这种方式在列中有一个值

"id=Clarizen,ou=GROUP,dc=opensso,dc=java,dc=net|id=devendrat,ou=USER,dc=opensso,dc=java,dc=net"

我想从此字符串中提取组名和用户名,并将其存储到另一个表的单独列中。

期望的结果:

Clarizen 作为组名 devendrat 作为用户名

请帮忙

4

2 回答 2

0

您正在寻找 CharIndex 和 Substring 选项。以下适用于 T-SQL。我不确定 My SQL 中的语法

SELECT REPLACE(SUBSTRING(ColumnName,1,CHARINDEX(',',ColumnName) - 1),'ID=','') 
       AS Groupname,
       REPLACE(SUBSTRING(SUBSTRING(ColumnName,CHARINDEX('|',ColumnName),
       LEN(ColumnName)),1,
       CHARINDEX(',',ColumnName) - 1),'|ID=','') AS Username 
于 2013-06-18T06:09:42.970 回答
-1

(sorry this is C# I overlooked that you are using mysql, so the answer is useless to you but I'll leave it here unless someone is to remove it)

Using string split can get the job done, here is something that I whipped together, it won't be optimal but it definately works!

        string parse_me = "id=Clarizen,ou=GROUP,dc=opensso,dc=java,dc=net|id=devendrat,ou=USER,dc=opensso,dc=java,dc=net";

        string[] lines = parse_me.Split(',');

        List<string> variables = new List<string>();
        List<string> values = new List<string>();

        foreach (string line in lines)
        {
            string[] pair = line.Split('=');
            //Console.WriteLine(line);
            variables.Add(pair[0]);
            values.Add(pair[1]);
        }

        string group = "";
        string user = "";

        if (variables.Count == values.Count)
        {
            for (int i = 0; i < variables.Count; ++i )
            {
                Console.Write(variables[i]);
                Console.Write(" : ");
                Console.WriteLine(values[i]);

                if (variables[i] == "ou")
                {
                    if (group == "")
                    {
                        group = values[i];
                    }
                    else if (user == "")
                    {
                        user = values[i];
                    }
                }
            }
        }

        Console.WriteLine("Group is: " + group);
        Console.WriteLine("User is: " + user);

        Console.ReadLine();
于 2013-06-18T06:12:49.783 回答