diff --git a/Src/Asp.Net/MySqlTest/Config.cs b/Src/Asp.Net/MySqlTest/Config.cs index 48e302503..b3b355553 100644 --- a/Src/Asp.Net/MySqlTest/Config.cs +++ b/Src/Asp.Net/MySqlTest/Config.cs @@ -8,8 +8,8 @@ namespace OrmTest { public class Config { - public static string ConnectionString = "server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=haosql"; - public static string ConnectionString2 = "server=localhost;Database=SQLSUGAR4XTEST;Uid=root;Pwd=haosql"; - public static string ConnectionString3 = "server=localhost;Database=sqlsugar4xtest;Uid=root;Pwd=haosql"; + public static string ConnectionString = "server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=root"; + public static string ConnectionString2 = "server=localhost;Database=SQLSUGAR4XTEST;Uid=root;Pwd=root"; + public static string ConnectionString3 = "server=localhost;Database=sqlsugar4xtest;Uid=root;Pwd=root"; } } diff --git a/Src/Asp.Net/MySqlTest/Demos/1_Query.cs b/Src/Asp.Net/MySqlTest/Demos/1_Query.cs index b3ba02b5e..8b4982839 100644 --- a/Src/Asp.Net/MySqlTest/Demos/1_Query.cs +++ b/Src/Asp.Net/MySqlTest/Demos/1_Query.cs @@ -58,13 +58,13 @@ namespace OrmTest.Demo id = SqlFunc.Subqueryable().Where(s => s.Id == st.Id).Select(s => s.Id) }) .ToList(); - + int count = 0; var getAll4 = db.Queryable().Select(it => new { name = it.Name, id = SqlFunc.Subqueryable().Where(s => s.Id == it.Id).Select(s => s.Id) - }).ToList(); + }).ToPageList(1, 2, ref count); var getAll5 = db.Queryable().Select(it => new Student diff --git a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlQueryBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlQueryBuilder.cs index a9a81ad57..27432a327 100644 --- a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlQueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlQueryBuilder.cs @@ -63,6 +63,36 @@ namespace SqlSugar this.OrderByValue = oldOrderValue; return result; } + private string ToCountSqlString() + { + base.AppendFilter(); + string oldOrderValue = this.OrderByValue; + string result = null; + sql = new StringBuilder(); + sql.AppendFormat(SqlTemplate, "Count(*)", GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString); + if (IsCount) { return sql.ToString(); } + if (Skip != null && Take == null) + { + if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0]; + result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString, Skip.ObjToInt(), long.MaxValue); + } + else if (Skip == null && Take != null) + { + if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0]; + result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, 0, Take.ObjToInt()); + } + else if (Skip != null && Take != null) + { + if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0]; + result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() : 0, Take); + } + else + { + result = sql.ToString(); + } + this.OrderByValue = oldOrderValue; + return result; + } public override string ToCountSql(string sql) { if (this.GroupByValue.HasValue()) @@ -71,7 +101,7 @@ namespace SqlSugar } else { - return Regex.Replace(sql, "^SELECT .+? FROM ", "SELECT COUNT(*) FROM "); + return ToCountSqlString(); } } #endregion