From 07a2bb72a1f3bf899e87ba19446cabc390643eba Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Fri, 10 Mar 2023 20:34:26 +0800 Subject: [PATCH] update --- API.md | 1 + Update-Data.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++- 修改.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 101 insertions(+), 2 deletions(-) diff --git a/API.md b/API.md index 162bcb1..bf68dfd 100644 --- a/API.md +++ b/API.md @@ -233,6 +233,7 @@ DbContext 自身 = 完整事务,BaseRepository 不一定有事务(可通过 | ToSql | string | | 返回即将执行的SQL语句 | | ExecuteAffrows | long | | 执行SQL语句,返回影响的行数 | | ExecuteUpdated | List\ | | 执行SQL语句,返回更新后的记录 | +| Join | IUpdateJoin | | 联表更新 | --- diff --git a/Update-Data.md b/Update-Data.md index f81d6a9..75563a4 100644 --- a/Update-Data.md +++ b/Update-Data.md @@ -250,7 +250,55 @@ The benefits of using this program for dang complex update: - Data can be previewed before updating to prevent wrong update operations; - Support complex update operations, for example: Use `Limit(10)` on `ISelect` to update the first 10 records that meet the conditions; -## 11、BulkCopy Batch Update +## 11、UpdateJoin + +v3.2.692+ (High risk operation, high risk operation, high risk operation, please use with caution, test and check the content returned by ToSql) + +```csharp +fsql.Update() + .Join((a, b) => a.id == b.groupid) + .Set((a, b) => a.bname == b.name) + .Set((a, b) => a.bcode == b.id + a.code) + .Set(a => a.flag, 1) //Fixed value + .Where((a, b) => a.id > 0 && b.id > 0) + .ExecuteAffrows(); +``` + +The SQL generated by different databases is different. Take MySql as an example: + +```sql +UPDATE `T1` a +INNER JOIN `T2` b ON (a.`id` = b.`groupid`) +SET a.`bname` = b.`name`, a.`bcode` = concat(b.`id`, a.`code`), a.`flag` = 1 +WHERE a.`id` > 0 AND b.`id` > 0 +``` + +More complex joint table update: + +```csharp +var query = fsql.Select() + .InnerJoin(...) + .Where(...) + .WithTempQuery((a, b) => new { item1 = a, item2 = b }); + +fsql.Update() + .Join(query, (a, b) => a.id == b.item1.groupid) + .Set((a, b) => a.bcode == b.item2.xcode) + .ExecuteAffrows(); +``` + +```sql +UPDATE `T1` a +INNER JOIN ( + SELECT ... + FROM `t2` a + INNER JOIN ... + Where ... +) b ON (a.`id` = b.`groupid`) +SET a.`bcode` = b.`xcode` +``` + +## 12、BulkCopy Batch Update FreeSql.Provider.SqlServer、FreeSql.Provider.MySqlConnector、FreeSql.Provider. PostgreSQL @@ -293,3 +341,4 @@ fsql.Update().SetSource(list).ExecuteSqlBulkCopy(); | ToSql | string | | Return the SQL statement to be executed | | ExecuteAffrows | long | | Execute SQL statement and return the number of rows affected | | ExecuteUpdated | List\ | | Execute SQL statement and return the updated record | +| Join | IUpdateJoin | | 联表更新 | diff --git a/修改.md b/修改.md index 77edf9c..401c714 100644 --- a/修改.md +++ b/修改.md @@ -253,7 +253,55 @@ UPDATE `T1` SET Title = '111' WHERE id in (select a.id from T1 a left join Optio - 更新前可预览测试数据,防止错误更新操作; - 支持复杂的更新操作,例如:`ISelect` 上使用 `Limit(10)` 更新附合条件的前 10 条记录; -## 11、BulkCopy 批量更新 +## 11、联表更新 UpdateJoin + +v3.2.692+(高风险操作,高风险操作,高风险操作,请谨慎谨慎谨慎使用,测试并核对 ToSql 返回的内容) + +```csharp +fsql.Update() + .Join((a, b) => a.id == b.groupid) + .Set((a, b) => a.bname == b.name) //其他表字段 + .Set((a, b) => a.bcode == b.id + a.code) + .Set(a => a.flag, 1) //固定值 + .Where((a, b) => a.id > 0 && b.id > 0) + .ExecuteAffrows(); +``` + +不同数据库产生的 SQL 不一样,以 MySql 为例: + +```sql +UPDATE `T1` a +INNER JOIN `T2` b ON (a.`id` = b.`groupid`) +SET a.`bname` = b.`name`, a.`bcode` = concat(b.`id`, a.`code`), a.`flag` = 1 +WHERE a.`id` > 0 AND b.`id` > 0 +``` + +更复杂的联表更新: + +```csharp +var query = fsql.Select() + .InnerJoin(...) + .Where(...) + .WithTempQuery((a, b) => new { item1 = a, item2 = b }); + +fsql.Update() + .Join(query, (a, b) => a.id == b.item1.groupid) + .Set((a, b) => a.bcode == b.item2.xcode) + .ExecuteAffrows(); +``` + +```sql +UPDATE `T1` a +INNER JOIN ( + SELECT ... + FROM `t2` a + INNER JOIN ... + Where ... +) b ON (a.`id` = b.`groupid`) +SET a.`bcode` = b.`xcode` +``` + +## 12、BulkCopy 批量更新 FreeSql.Provider.SqlServer、FreeSql.Provider.MySqlConnector、FreeSql.Provider.PostgreSQL @@ -296,3 +344,4 @@ fsql.Update().SetSource(list).ExecuteSqlBulkCopy(); | ToSql | string | | 返回即将执行的 SQL 语句 | | ExecuteAffrows | long | | 执行 SQL 语句,返回影响的行数 | | ExecuteUpdated | List\ | | 执行 SQL 语句,返回更新后的记录 | +| Join | IUpdateJoin | | 联表更新 |