一个字符串的操作

yaotomo 2019-04-06 07:40:58
一个mysql的查询语句字符串,要求按下述条件处理。limit表示取数据库中的多少行,比如limit 100就是取查询结果的前100行。limit 50 100就是取查询结果的50到100行。如果字符串以'limit 数字'或'limit 数字 数字'结尾,保持不变,否则在sql语句末尾添加limit 10000。
举个例子
"select * from table1 limit 50"
"select * from table1 limit 50 100"
"select * from table2 where col in (select col from table2 limit 50) limit 100 200"
这几个字符串处理后没有变化
"select * from table1"
"select * from table1 where col='val'"
"select * from table2 where col in (select col from table2 limit 50)"
处理后分别是
"select * from table1 limit 10000"
"select * from table1 where col='val' limit 10000"
"select * from table2 where col in (select col from table2 limit 50) limit 10000"
...全文
304 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaotomo 2019-04-11
  • 打赏
  • 举报
回复
引用 7 楼 3ch0 的回复:

static string Format(string sqlStr)
{
    string result = "";
    //移除字符串中的数字
    var removeN = Regex.Replace(sqlStr, @"\d", "").Trim();
    //移除后结果如果以limit结尾 说明已进行了取数据行
    if (removeN.EndsWith("limit"))
    {
        //返回原始语句
        result = sqlStr.Trim();
    }
    else
    {
        //返回处理后的语句
        result = sqlStr.Trim() + " limit 10000";
    }
    return result;
}
谢谢,给我提供了一个新的思路!
weixin_42239780 2019-04-08
  • 打赏
  • 举报
回复
收藏备用~~
elvisv 2019-04-08
  • 打赏
  • 举报
回复
引用 6 楼 yaotomo 的回复:
[quote=引用 3 楼 elvisv 的回复:] limit的理解有误, 50,100不是从五十到一百,是从五十开始选一百条
就是50到100条之间的数据啊 mysql分页都是这么做的 还有,我的字符串不是limit 50 100,而是limit 数字 数字,50和100只是举例,因此不能写死[/quote] limit 50,100 不等于50-100,而是等于50-150
csdnFUCKINGSUCKS 2019-04-08
  • 打赏
  • 举报
回复

static string Format(string sqlStr)
{
    string result = "";
    //移除字符串中的数字
    var removeN = Regex.Replace(sqlStr, @"\d", "").Trim();
    //移除后结果如果以limit结尾 说明已进行了取数据行
    if (removeN.EndsWith("limit"))
    {
        //返回原始语句
        result = sqlStr.Trim();
    }
    else
    {
        //返回处理后的语句
        result = sqlStr.Trim() + " limit 10000";
    }
    return result;
}
yaotomo 2019-04-08
  • 打赏
  • 举报
回复
引用 3 楼 elvisv 的回复:
limit的理解有误, 50,100不是从五十到一百,是从五十开始选一百条
就是50到100条之间的数据啊 mysql分页都是这么做的 还有,我的字符串不是limit 50 100,而是limit 数字 数字,50和100只是举例,因此不能写死
yaotomo 2019-04-08
  • 打赏
  • 举报
回复
引用 1 楼 xqing0101 的回复:

public string getstr(string str)
{
    if (str.LastIndexOf("limit 50)") > -1)
        return str + " limit 10000";
    else if (str.LastIndexOf("limit 50") != -1)
        return str;
    else if (str.LastIndexOf("limit 50 100") != -1)
        return str;
    else if (str.LastIndexOf("limit 100 200") != -1)
        return str;
    else
        return str + " limit 10000";
}

Console.WriteLine(p.getstr("select * from table1"));
Console.WriteLine(p.getstr("select * from table1 where col='val'"));
Console.WriteLine(p.getstr("select * from table2 where col in (select col from table2 limit 50)"));
不是这么简单的 50和100只是举例,只要是数字都要这么用 比如limit 20 30,limit 60,120
qiu_n_xin_z 2019-04-08
  • 打赏
  • 举报
回复
收藏一下~~
elvisv 2019-04-08
  • 打赏
  • 举报
回复
limit的理解有误, 50,100不是从五十到一百,是从五十开始选一百条
xqing0101 2019-04-07
  • 打赏
  • 举报
回复

public string getstr(string str)
{
    if (str.LastIndexOf("limit 50)") > -1)
        return str + " limit 10000";
    else if (str.LastIndexOf("limit 50") != -1)
        return str;
    else if (str.LastIndexOf("limit 50 100") != -1)
        return str;
    else if (str.LastIndexOf("limit 100 200") != -1)
        return str;
    else
        return str + " limit 10000";
}

Console.WriteLine(p.getstr("select * from table1"));
Console.WriteLine(p.getstr("select * from table1 where col='val'"));
Console.WriteLine(p.getstr("select * from table2 where col in (select col from table2 limit 50)"));

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧