在Ado.Net的两列中加入两行具有相同值的行

时间:2013-02-25 17:23:46

标签: mysql winforms ado.net group-concat

对于这个问题,Join two rows which have same value in two column感谢吉姆。但是我没有在windows窗体应用程序中使用下面的代码,但是并没有实现。这是MySQL代码:

 SELECT MIN(id),
       fikraNo,
       maddeNo,
       GROUP_CONCAT(icerik ORDER BY id DESC SEPARATOR ' ')
FROM `table`
GROUP BY fikraNo, maddeNo 

这是我使用MySQL的搜索方法:

public static DataTable Results(string table, string columns, string words)
{
    var sb = new StringBuilder();
    //var pm = new SqlParameter();

    sb.Append("SELECT MIN(id) AS minID, name, surname, GROUP_CONCAT(events ORDER BY id ASC SEPARATOR ' ') AS allEvents FROM " + table + " ");
    if (!string.IsNullOrEmpty(words))
    {
        var column = columns.Split('+');
        for (var i = 0; i < column.Length; i++)
        {
            if (i == 0)
            {
                var word = words.Split(',');
                for (var j = 0; j < word.Length; j++)
                {
                    if (j == 0)
                    {
                        // one column, one word...
                        sb.Append("WHERE " + column[i].Trim() + " LIKE '%" + Strip(word[j].Trim()) + "%'");
                        //pm = new SqlParameter("@word", "%" + word[j].Trim() + "%");
                    }
                    else
                    {
                        // one column, more words..
                        sb.Append(" OR " + column[i].Trim() + " LIKE '%" + Strip(word[j].Trim()) + "%'");
                        //pm = new SqlParameter("@word", "%" + word[j].Trim() + "%");
                    }

                }
            }
            else
            {
                var word = words.Split(',');
                for (var j = 0; j < word.Length; j++)
                {
                    if (j == 0)
                    {
                        // more columns, one word...
                        sb.Append(" OR " + column[i].Trim() + " LIKE '%" + Strip(word[j].Trim()) + "%'");
                        //pm = new SqlParameter("@word", "%" + word[j].Trim() + "%");
                    }
                    else
                    {
                        // more columns, more words...
                        sb.Append(" OR " + column[i].Trim() + " LIKE '%" + Strip(word[j].Trim()) + "%'");
                        //pm = new SqlParameter("@word", "%" + word[j].Trim() + "%");
                    }

                }
            }
        }
    }
    sb.Append(" GROUP BY name, surname");

    var dt = SetDataset(CommandType.Text, sb.ToString(), null).Tables[0];
    return dt;
}

但是,不幸的是我的方法只带来第一排。 你能帮助我吗,我该怎么办呢? 金问候。

0 个答案:

没有答案