mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-03 23:10:54 +08:00
update
@@ -100,7 +100,7 @@ fsql.Select<Topic, Category, CategoryType>()
|
||||
|
||||
> 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
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ fsql.Select<Topic>()
|
||||
|
||||
> 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
|
||||
|
||||
|
||||
@@ -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<Topic>()
|
||||
|
||||
> 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
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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) ✨
|
||||
|
||||
@@ -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,如下:
|
||||
|
||||
2
单表查询.md
2
单表查询.md
@@ -52,7 +52,7 @@ fsql.Select<Topic>()
|
||||
|
||||
> 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 使用内存数据进行查询
|
||||
|
||||
|
||||
2
嵌套查询.md
2
嵌套查询.md
@@ -6,6 +6,8 @@
|
||||
|
||||
需求版本:v3.2.666-preview (最新)
|
||||
|
||||
GroupBy + WithTempQuery(嵌套查询) + FromQuery + UnionAll 组合使用,会让查询功能更加强大、灵活。
|
||||
|
||||
#### 场景1:查询分组第一条记录
|
||||
|
||||
```csharp
|
||||
|
||||
1
更新日志.md
1
更新日志.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
|
||||
|
||||
108
联合查询.md
Normal file
108
联合查询.md
Normal file
@@ -0,0 +1,108 @@
|
||||
在之前都是推荐使用 ToSql + WithSql 完成联合查询操作,v3.2.666 最增功能直接使用 UnionAll 方法。
|
||||
|
||||
GroupBy + WithTempQuery(嵌套查询) + FromQuery + UnionAll 组合使用,会让查询功能更加强大、灵活。
|
||||
|
||||
1、单表 UNION ALL
|
||||
|
||||
```csharp
|
||||
var sql = fsql.Select<User>().Where(a => a.Id == 1)
|
||||
.UnionAll(
|
||||
fsql.Select<User>().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<User, Group>()
|
||||
.Where((a, b) => a.Id == 1)
|
||||
.WithTempQuery((a, b) => new { user = a, group = b }) //匿名类型
|
||||
|
||||
.UnionAll(
|
||||
fsql.Select<User, Group>()
|
||||
.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<DbParameter>();
|
||||
|
||||
var id1 = 1;
|
||||
var id2 = 2;
|
||||
var sql = fsql.Select<User>()
|
||||
.WithParameters(dbpars)
|
||||
.Where(a => a.Id == id1)
|
||||
|
||||
.UnionAll(
|
||||
fsql.Select<User>()
|
||||
.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))
|
||||
```
|
||||
4
返回数据.md
4
返回数据.md
@@ -82,6 +82,8 @@ List<匿名类> t11 = fsql.Select<Topic>().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<Topic>()
|
||||
|
||||
> 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 使用内存数据进行查询
|
||||
|
||||
|
||||
2
骚操作.md
2
骚操作.md
@@ -86,7 +86,7 @@ fsql.Select<Topic>()
|
||||
|
||||
> 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 使用内存数据进行查询
|
||||
|
||||
|
||||
Reference in New Issue
Block a user