From 08d2674ad4612f44c3c9aed6337b240cd7056957 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Fri, 3 Mar 2023 10:43:56 +0800 Subject: [PATCH] update --- 嵌套查询.md | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/嵌套查询.md b/嵌套查询.md index 8c871f2..f62b641 100644 --- a/嵌套查询.md +++ b/嵌套查询.md @@ -141,7 +141,7 @@ list.Add(new User1 { Id = Guid.NewGuid() }); list.Add(new User1 { Id = Guid.NewGuid() }); list.Add(new User1 { Id = Guid.NewGuid() }); -var listSql2 = fsql.Select() +fsql.Select() .FromQuery(fsql.Select().WithMemory(list)) .InnerJoin((a, b) => a.Id == b.GroupId) .ToSql(); @@ -164,7 +164,7 @@ INNER JOIN ( 自动分表后,如果有分页的需求 或者分组聚合的需求可以参考以下代码 ```csharp -var result = fsql.Select() +fsql.Select() .Where(a => a.createtime.BetweenEnd(startTime, endTime)) //时间字段定位表 .WithTempQuery(a => new { item = a }) .GroupBy(a => a.item.shareId) @@ -198,6 +198,48 @@ GROUP BY a.`shareId` LIMIT 0,30 ``` +#### 场景6:FromQuery 多个查询,最后映射查询 + +```csharp +var query2 = fsql.Select() + .InnerJoin((a, b, c) => a.LoadNo == b.LoadNo && a.UnitTransactionType == "TO") + .InnerJoin((a, b, c) => b.InstructionNo == c.InstructionNo) + .WithTempQuery((a, b, c) => new + { + a.LoadNo, + a.SeqNoLog, + c.DeliveryInstractionStatus, + c.UpTime, + RN = SqlExt.RowNumber().Over().PartitionBy(a.UnitId).OrderByDescending(a.SeqNoLog).ToValue() + }); +var query3 = fsql.Select(); + +fsql.Select() + .FromQuery(query2, query3) + .InnerJoin((a,b,c) => a.SeqNoLog == b.SeqNoLog) + .InnerJoin((a,b,c) => a.UnitId == c.UnitId) + .Where((a,b,c) => b.RN < 2) + .ToSql((a,b,c) => new MB51_View + { + //CkassIfCation = a.CkassIfCation, + PGI = b.DeliveryInstractionStatus, + PGITime = b.UpTime, + IsDelayPGI = true, + RunNo = c.RunNo + }); +``` + +```sql +SELECT a.[CkassIfCation] as1, b.[DeliveryInstractionStatus] as2, b.[UpTime] as3, 1 as4, c.[RunNo] as5 +FROM [UnitLog] a +INNER JOIN (SELECT a.[LoadNo], a.[SeqNoLog], c.[DeliveryInstractionStatus], c.[UpTime], row_number() over( partition by a.[UnitId] order by a.[SeqNoLog] desc) [RN] + FROM [UnitLog] a + INNER JOIN [LoadPlan] b ON a.[LoadNo] = b.[LoadNo] AND a.[UnitTransactionType] = N'TO' + INNER JOIN [Instruction] c ON b.[InstructionNo] = c.[InstructionNo] ) b ON a.[SeqNoLog] = b.[SeqNoLog] +INNER JOIN [Unit] c ON a.[UnitId] = c.[UnitId] +WHERE (b.[RN] < 2) +``` + ## WithParameters 参数化共享 开启参数化查询功能后,使用 WithParameters 共享参数化,避免产生相同的参数名称: