update

2881099
2022-04-30 11:03:31 +08:00
parent 2707f7ad5b
commit 0255fe7df9
2 changed files with 108 additions and 109 deletions

@@ -183,14 +183,11 @@ fsql.Ado.CommandFluent($"{sql1} UNION ALL {sql2}")
.ExecuteDataTable();
```
## Paging Problem
After using `UNION ALL`, there will be a problem if you paginate directly. Please see the specific example:
### There is a problem with using WithSql + Page multiple times: There is a paging statement in each WithSql
There is a problem with using WithSql + Page multiple times: There is a paging statement in each WithSql
```csharp
var sql1 = fsql.Select<Topic>()
@@ -205,18 +202,18 @@ fsql.Select<Topic>().WithSql(sql1).WithSql(sql2).Page(1, 20).ToList();
```sql
SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM `tb_topic` a
WHERE ((a.`Title`) LIKE '%xxx%') ) a
limit 0,20) ftb
limit 0,20) ftb
UNION ALL
UNION ALL
SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM `tb_topic` a
WHERE ((a.`Title`) LIKE '%yyy%') ) a
limit 0,20) ftb
limit 0,20) ftb
```
@@ -240,7 +237,7 @@ Call WithSql multiple times. If you need to paging, you need to follow the two s
The above code will be generated as a Sql statement using `UNION ALL`:
```sql
SELECT * from (SELECT *
SELECT * from (SELECT *
FROM ( SELECT * FROM tb_topic where id > 11 ) a) ftb
UNION ALL

@@ -187,13 +187,12 @@ fsql.Ado.CommandFluent($"{sql1} UNION ALL {sql2}")
.ExecuteDataTable();
```
## 分页问题
Union All 之后 如果直接 分页会有一个问题。请看具体示例
多次 WithSql + Page 存在问题每个WithSql内都有一个Page分页
### 多次WithSql+Page存在问题每个WithSql内都有一个Page分页
```csharp
var sql1 = fsql.Select<Topic>()
.Where(a => a.Title.Contains("xxx"))
@@ -207,18 +206,18 @@ fsql.Select<Topic>().WithSql(sql1).WithSql(sql2).Page(1, 20).ToList();
```sql
SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM `tb_topic` a
WHERE ((a.`Title`) LIKE '%xxx%') ) a
limit 0,20) ftb
limit 0,20) ftb
UNION ALL
UNION ALL
SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM `tb_topic` a
WHERE ((a.`Title`) LIKE '%yyy%') ) a
limit 0,20) ftb
limit 0,20) ftb
```
@@ -226,11 +225,12 @@ limit 0,20) ftb
WithSql 可以和 AsTable 实现分表的功能。
分表跨表查询的时候分页是要向每个子表即每个WithSql中的SQL分页都生效。
分表跨表查询的时候,分页是要向每个子表(即每个 WithSql 中的 SQL 分页)都生效。
## 解决方案
### 多次withsql如需分页需要按下面的二步操作
多次 withsql ,如需分页,需要按下面的二步操作
- 第一步通过witsql将二个sql组成一个sql。
```csharp
@@ -240,10 +240,10 @@ WithSql 可以和 AsTable 实现分表的功能。
.ToSql("*")
```
如上生成的UOION ALLsql
如上生成的 UOION ALLsql
```sql
SELECT * from (SELECT *
SELECT * from (SELECT *
FROM ( SELECT * FROM tb_topic where id > 11 ) a) ftb
UNION ALL
@@ -252,13 +252,15 @@ WithSql 可以和 AsTable 实现分表的功能。
FROM ( SELECT * FROM tb_topic where id < 10 ) a) ftb
```
- 第二步:之后 调用Page则是通过Union ALL后的结果上分页
- 第二步:之后 调用 Page 则是通过 Union ALL 后的结果上分页
```csharp
var sql2 = g.mysql.Select<Topic>()
.WithSql(sql)
.Page(2, 10)
.ToSql();
```
```sql
SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
FROM ( SELECT * from (SELECT *