From 6a2c404dfbae6968cbcbbcb889fbcc57e809ccb0 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Fri, 26 Aug 2022 17:52:15 +0800 Subject: [PATCH] update --- Query-from-Multi-Tables.md | 2 +- Query-from-Single-Table.md | 2 +- Return-Data.md | 4 +- With-Sql.md | 2 + _Sidebar.md | 3 +- withsql.md | 2 + 单表查询.md | 2 +- 嵌套查询.md | 2 + 更新日志.md | 1 + 联合查询.md | 108 +++++++++++++++++++++++++++++++++++++ 返回数据.md | 4 +- 骚操作.md | 2 +- 12 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 联合查询.md diff --git a/Query-from-Multi-Tables.md b/Query-from-Multi-Tables.md index fec49c5..cbef208 100644 --- a/Query-from-Multi-Tables.md +++ b/Query-from-Multi-Tables.md @@ -100,7 +100,7 @@ fsql.Select() > Tip: `ISelect.ToSql` can be used with `WithSql` -> v3.2.666 [WithTempQuery + FromQuery Nested Query](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) +> v3.2.666 [UnionAll Query](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)、[WithTempQuery + FromQuery Nested Query](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) > v3.2.666 WithMemory Query using memory data diff --git a/Query-from-Single-Table.md b/Query-from-Single-Table.md index bf1b9c0..d21ee7a 100644 --- a/Query-from-Single-Table.md +++ b/Query-from-Single-Table.md @@ -55,7 +55,7 @@ fsql.Select() > When `WithSql` is used multiple times, it will be converted to `UNION ALL` query -> v3.2.666 [WithTempQuery + FromQuery Nested Query](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) +> v3.2.666 [UnionAll Query](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)、[WithTempQuery + FromQuery Nested Query](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) > v3.2.666 WithMemory Query using memory data diff --git a/Return-Data.md b/Return-Data.md index c39dc7f..deb5fab 100644 --- a/Return-Data.md +++ b/Return-Data.md @@ -93,6 +93,8 @@ All `ToList` can use `ToSql` to return SQL string. There are two options: - FieldAliasOptions.AsIndex, the default option, automatically generates as1, as2, as3 .... etc. field aliases, which can prevent the problem of multiple tables with the same field. - FieldAliasOptions.AsProperty, use the property name as the field alias, appropriately use the two-stage structure SQL and execute it again. +> v3.2.666 After the parameterized query function is enabled, use withparameters to share parameterization to avoid multiple query objects from generating the same parameter name, for example: [UnionAll Query](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2) + ## Executing SQL ```csharp @@ -123,7 +125,7 @@ fsql.Select() > When `WithSql` is used multiple times, `UNION ALL` query will be used -> v3.2.666 [WithTempQuery + FromQuery Nested Query](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) +> v3.2.666 [UnionAll Query](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)、[WithTempQuery + FromQuery Nested Query](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) > v3.2.666 WithMemory Query using memory data diff --git a/With-Sql.md b/With-Sql.md index 7cde3a0..ac5de03 100644 --- a/With-Sql.md +++ b/With-Sql.md @@ -129,6 +129,8 @@ SELECT ID, Age,BIRTH_DAY as Birthday ## WithSql+ ToSQL = Union ALL +> v3.2.666 [UnionAll Query](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2) + ### Two-Stage ISelect Query: Use WithSql Multiple Times to Convert to UNION ALL Query After using `WithSql` multiple times, a query statement based on `UNION ALL` will be generated. So we can use `ISelect.ToSql(FieldAliasOptions.AsProperty)` to get the generated SQL as follows: diff --git a/_Sidebar.md b/_Sidebar.md index 87377c2..65189a7 100644 --- a/_Sidebar.md +++ b/_Sidebar.md @@ -15,7 +15,8 @@ * [单表](%e5%8d%95%e8%a1%a8%e6%9f%a5%e8%af%a2) [Single Table](Query-from-Single-Table) * [多表](%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2) [Multi Tables](Query-from-Multi-Tables) * [分组聚合](%e5%88%86%e7%bb%84%e8%81%9a%e5%90%88%e6%9f%a5%e8%af%a2) [Group Aggregation](Group-Aggregation-Query) - * [嵌套查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) [Nested Query](Nested-Query) + * [嵌套查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) [Nested Query](Nested-Query) ✨ + * [联合查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) [Union Query](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) ✨ * [返回数据](%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae) [Return Data](Return-Data) ✨ * [延时加载](%e5%bb%b6%e6%97%b6%e5%8a%a0%e8%bd%bd) [Lazy Loading](Lazy-Loading) * [贪婪加载](%e8%b4%aa%e5%a9%aa%e5%8a%a0%e8%bd%bd) [Greed Loading](Greed-Loading) ✨ diff --git a/withsql.md b/withsql.md index 0458346..e4698a8 100644 --- a/withsql.md +++ b/withsql.md @@ -128,6 +128,8 @@ SELECT ID,Age,BIRTH_DAY as Birthday ## 通过 WithSql+ ToSQL实现 Union ALL 查询方法 +> v3.2.666 [UnionAll 联合查询](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2) + ### 1、二次 ISelect 查询:WithSql 使用多次,等于 UNION ALL 查询 WithSql 使用多次为 UNION ALL 查询,所以我们可以利用 ISelect.ToSql(FieldAliasOptions.AsProperty) 得到生成的 SQL,如下: diff --git a/单表查询.md b/单表查询.md index 9ee2327..4f82693 100644 --- a/单表查询.md +++ b/单表查询.md @@ -52,7 +52,7 @@ fsql.Select() > WithSql 使用多次为 UNION ALL 查询 -> v3.2.666 [WithTempQuery + FromQuery 嵌套查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) +> v3.2.666 [UnionAll 联合查询](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)、[WithTempQuery + FromQuery 嵌套查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) > v3.2.666 WithMemory 使用内存数据进行查询 diff --git a/嵌套查询.md b/嵌套查询.md index f63f9c1..cac0c5c 100644 --- a/嵌套查询.md +++ b/嵌套查询.md @@ -6,6 +6,8 @@ 需求版本:v3.2.666-preview (最新) +GroupBy + WithTempQuery(嵌套查询) + FromQuery + UnionAll 组合使用,会让查询功能更加强大、灵活。 + #### 场景1:查询分组第一条记录 ```csharp diff --git a/更新日志.md b/更新日志.md index 756edfc..9508bb9 100644 --- a/更新日志.md +++ b/更新日志.md @@ -4,6 +4,7 @@ - **增加 FreeSql.Provider.OracleOledb 解决 US7ASCII 中文乱码问题;** - **增加 WithTempQuery + FromQuery 嵌套查询功能;** #1192 +- ** 增加 UnionALL 联合查询;** #1106 #1104 #668 #478 #432 #213 #138 - 增加 WithMemory 基于内存查询,对标 WithSql; - 增加 AuditValue ObjectAuditBreak 实现对象只触发一次审计事件; - 增加 IncludeByPropertyName 重载 then 参数;#1214 diff --git a/联合查询.md b/联合查询.md new file mode 100644 index 0000000..dcce7cc --- /dev/null +++ b/联合查询.md @@ -0,0 +1,108 @@ +在之前都是推荐使用 ToSql + WithSql 完成联合查询操作,v3.2.666 最增功能直接使用 UnionAll 方法。 + +GroupBy + WithTempQuery(嵌套查询) + FromQuery + UnionAll 组合使用,会让查询功能更加强大、灵活。 + +1、单表 UNION ALL + +```csharp +var sql = fsql.Select().Where(a => a.Id == 1) + .UnionAll( + fsql.Select().Where(a => a.Id == 2) + ) + .Where(a => a.Id == 1 || a.Id == 2) + .ToSql(); +``` + +```sql +SELECT a."Id", a."GroupId", a."Username" +FROM ( SELECT a."Id", a."GroupId", a."Username" + FROM "User" a + WHERE (a."Id" = 1) + UNION ALL + SELECT a."Id", a."GroupId", a."Username" + FROM "User" a + WHERE (a."Id" = 2) ) a +WHERE ((a."Id" = 1 OR a."Id" = 2)) +``` + +2、多表 UNION ALL + +```csharp +var sql = fsql.Select() + .Where((a, b) => a.Id == 1) + .WithTempQuery((a, b) => new { user = a, group = b }) //匿名类型 + + .UnionAll( + fsql.Select() + .Where((a, b) => a.Id == 2) + .WithTempQuery((a, b) => new { user = a, group = b }) //匿名类型 + ) + .Where(a => a.user.Id == 1 || a.user.Id == 2) + .ToSql(); +``` + +```sql +SELECT * +FROM ( SELECT * + FROM ( + SELECT a."Id", a."GroupId", a."Username", b."Id", b."GroupName" + FROM "User" a + INNER JOIN "UserGroup" b ON a."GroupId" = b."Id" + WHERE (a."Id" = 1) ) a + UNION ALL + SELECT * + FROM ( + SELECT a."Id", a."GroupId", a."Username", b."Id", b."GroupName" + FROM "User" a + INNER JOIN "UserGroup" b ON a."GroupId" = b."Id" + WHERE (a."Id" = 2) ) a ) a +WHERE ((a."Id" = 1 OR a."Id" = 2)) +``` + +注意:如上 SQL 会执行报错,因为 User、UserGroup 都存在相同的 Id 字段名称,暂时的解决办法需要指定字段 + +```csharp + .WithTempQuery((a, b) => new + { + user = a, + group = new + { + GroupId = b.Id, + GroupName = b.GroupName + } + }) +``` + +3、WithParameters 参数化共享 + +开启参数化查询功能后,使用 WithParameters 共享参数化,避免产生相同的参数名称: + +```csharp +var dbpars = new List(); + +var id1 = 1; +var id2 = 2; +var sql = fsql.Select() + .WithParameters(dbpars) + .Where(a => a.Id == id1) + + .UnionAll( + fsql.Select() + .WithParameters(dbpars) + .Where(a => a.Id == id2) + ) + .Where(a => a.Id == 1 || a.Id == 2) + .ToSql(); +``` + +```sql +SELECT a."Id", a."GroupId", a."Username" +FROM ( SELECT a."Id", a."GroupId", a."Username" + FROM "User1" a + WHERE (a."Id" = @exp_0) + UNION ALL + SELECT a."Id", a."GroupId", a."Username" + FROM "User1" a + WHERE (a."Id" = @exp_1) ) a +WHERE ((a."Id" = 1 OR a."Id" = 2)) +``` diff --git a/返回数据.md b/返回数据.md index 0011fb5..a566724 100644 --- a/返回数据.md +++ b/返回数据.md @@ -82,6 +82,8 @@ List<匿名类> t11 = fsql.Select().ToList(a => new { - FieldAliasOptions.AsIndex(默认) 自动产生 as1, as2, as3 .... 字段别名,可以最大程度防止多表,存在相同字段的问题; - FieldAliasOptions.AsProperty 使用属性名作为字段别名,合适使用二次构造 SQL 再次执行; +> v3.2.666 开启参数化查询功能后,使用 WithParameters 共享参数化,避免多个查询对象产生相同的参数名称,例如:[UnionAll 联合查询](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2) + ## 8、执行SQL ```csharp class xxx { @@ -109,7 +111,7 @@ fsql.Select() > WithSql 使用多次为 UNION ALL 查询 -> v3.2.666 [WithTempQuery + FromQuery 嵌套查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) +> v3.2.666 [UnionAll 联合查询](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)、[WithTempQuery + FromQuery 嵌套查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) > v3.2.666 WithMemory 使用内存数据进行查询 diff --git a/骚操作.md b/骚操作.md index d8badd3..0400b34 100644 --- a/骚操作.md +++ b/骚操作.md @@ -86,7 +86,7 @@ fsql.Select() > WithSql 使用多次为 UNION ALL 查询 -> v3.2.666 [WithTempQuery + FromQuery 嵌套查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) +> v3.2.666 [UnionAll 联合查询](%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2)、[WithTempQuery + FromQuery 嵌套查询](%e5%b5%8c%e5%a5%97%e6%9f%a5%e8%af%a2) > v3.2.666 WithMemory 使用内存数据进行查询