From 642c6ee856e2074a1ebb41182bd2b56d7ae4f084 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Wed, 11 Mar 2020 11:42:02 +0800 Subject: [PATCH] update --- 分页查询.md | 8 +-- 单表查询.md | 41 +++++++------- 多表查询.md | 153 ++++++++++++++++++++++++++-------------------------- 返回数据.md | 12 +++-- 骚操作.md | 16 +++--- 5 files changed, 117 insertions(+), 113 deletions(-) diff --git a/分页查询.md b/分页查询.md index c81b324..27ef2e7 100644 --- a/分页查询.md +++ b/分页查询.md @@ -3,14 +3,14 @@ static IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10") .Build(); //请务必定义成 Singleton 单例模式 -[Table(Name = "tb_topic")] class Topic { - [Column(IsIdentity = true, IsPrimary = true)] + [Column(IsIdentity = true)] public int Id { get; set; } - public int Clicks { get; set; } - public int TestTypeInfoGuid { get; set; } public string Title { get; set; } + public int Clicks { get; set; } public DateTime CreateTime { get; set; } + + public int CategoryId { get; set; } } ``` diff --git a/单表查询.md b/单表查询.md index 351360d..32303f3 100644 --- a/单表查询.md +++ b/单表查询.md @@ -3,49 +3,50 @@ static IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10") .Build(); //请务必定义成 Singleton 单例模式 -[Table(Name = "tb_topic")] class Topic { - [Column(IsIdentity = true, IsPrimary = true)] + [Column(IsIdentity = true)] public int Id { get; set; } - public int Clicks { get; set; } - public int TestTypeInfoGuid { get; set; } public string Title { get; set; } + public int Clicks { get; set; } public DateTime CreateTime { get; set; } + + public int CategoryId { get; set; } } ``` ## 单表 ```csharp -var sql = fsql.Select() +fsql.Select() .Where(a => a.Id == 10) - .ToSql(); -///SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` -//FROM `tb_topic` a + .ToList(); +///SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime` +//FROM `Topic` a //WHERE (a.`Id` = 10) -sql = fsql.Select() +fsql.Select() .Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100) - .ToSql(); -///SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` -//FROM `tb_topic` a + .ToList(); +///SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime` +//FROM `Topic` a //WHERE (a.`Id` = 10 AND a.`Id` > 10 OR a.`Clicks` > 100) -sql = fsql.Select() +fsql.Select() .Where(a => new []{1,2,3}.Contains(a.Id)) - .ToSql(); -//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` -//FROM `tb_topic` a + .ToList(); +//SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime` +//FROM `Topic` a //WHERE (a.`Id` in (1,2,3)) ``` -## 巧用AsTable +## WithSql ```csharp -var sql = fsql.Select() - .AsTable((a, b) => "(select * from user where clicks > 10)") +fsql.Select() + .WithSql("select * from Topic where clicks > 10") .Page(1, 10) .ToList() +//SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime` +//FROM (select * from Topic where clicks > 10) a ``` -> v1.0.1 ISelect 增加 WithSql("select * from user ...") 功能快速代理此方法 ## 参考资料 diff --git a/多表查询.md b/多表查询.md index d3395c2..0a40cea 100644 --- a/多表查询.md +++ b/多表查询.md @@ -3,24 +3,26 @@ static IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10") .Build(); //请务必定义成 Singleton 单例模式 -[Table(Name = "tb_topic")] class Topic { - [Column(IsIdentity = true, IsPrimary = true)] + [Column(IsIdentity = true)] public int Id { get; set; } - public int Clicks { get; set; } - public int TestTypeInfoGuid { get; set; } - public TestTypeInfo Type { get; set; } public string Title { get; set; } + public int Clicks { get; set; } public DateTime CreateTime { get; set; } + + public int CategoryId { get; set; } + public Category Category { get; set; } } -class TestTypeInfo { - public int Guid { get; set; } - public int ParentId { get; set; } - public TestTypeParentInfo Parent { get; set; } +class Category { + [Column(IsIdentity = true)] + public int Id { get; set; } public string Name { get; set; } + + public int ParentId { get; set; } + public CategoryType Parent { get; set; } public List Topics { get; set; } } -class TestTypeParentInfo { +class CategoryType { public int Id { get; set; } public string Name { get; set; } } @@ -28,115 +30,116 @@ class TestTypeParentInfo { ## 1、导航属性联表 ```csharp -sql = fsql.Select() - .LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid) - .LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId) - .ToSql(); -//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` -//FROM `tb_topic` a -//LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TestTypeInfoGuid` -//LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` +fsql.Select() + .LeftJoin(a => a.Category.Id == a.CategoryId) + .LeftJoin(a => a.Category.Parent.Id == a.Category.ParentId) + .ToList(); +//SELECT a.`Id`, a.`Title`, a.`Clicks`, a.`CreateTime`, a.`CategoryId`, a__Category.`Id` as6, a__Category.`Name`, a__Category.`ParentId` +//FROM `Topic` a +//LEFT JOIN `Category` a__Category ON a__Category.`Id` = a.`CategoryId` +//LEFT JOIN `CategoryType` a__Category__Parent ON a__Category__Parent.`Id` = a__Category.`ParentId` ``` > 提示:正确配置[导航关系](https://github.com/2881099/FreeSql/wiki/%e5%ae%9e%e4%bd%93%e5%85%b3%e7%b3%bb)后,不再需要手工调用 LeftJoin -## 2、普通联表 +## 2、复杂联表 ```csharp -sql = fsql.Select() - .LeftJoin((a, b) => b.Guid == a.TestTypeInfoGuid) - .ToSql(); -//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` -//FROM `tb_topic` a -//LEFT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TestTypeInfoGuid` -``` - -## 3、复杂联表 -```csharp -sql = fsql.Select().From((s, b, c) => s - .LeftJoin(a => a.TestTypeInfoGuid == b.Guid) - .LeftJoin(a => b.ParentId == c.Id)) - .ToSql(); +fsql.Select() + .LeftJoin((a,b,c) => a.CategoryId == b.Id) + .LeftJoin((a,b,c) => b.ParentId == c.Id) + .ToList(); //或者 -sql = fsql.Select() - .LeftJoin((a,b,c) => a.TestTypeInfoGuid == b.Guid) - .LeftJoin((a,b,c) => b.ParentId == c.id) - .ToSql(); -//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` -//FROM `tb_topic` a -//LEFT JOIN `TestTypeInfo` b ON a.`TestTypeInfoGuid` = b.`Guid` -//LEFT JOIN `TestTypeParentInfo` c ON b.`ParentId` = c.`Id` +fsql.Select().From((s, b, c) => s + .LeftJoin(a => a.CategoryId == b.Id) + .LeftJoin(a => b.ParentId == c.Id)) + .ToList(); +//SELECT a.`Id`, a.`Title`, a.`Clicks`, a.`CreateTime`, a.`CategoryId`, b.`Id` as6, b.`Name`, b.`ParentId` +//FROM `Topic` a +//LEFT JOIN `Category` b ON a.`CategoryId` = b.`Id` +//LEFT JOIN `CategoryType` c ON b.`ParentId` = c.`Id` ``` -## 4、SQL联表 +## 3、SQL联表 ```csharp -sql = fsql.Select() - .LeftJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = ?bname", new { bname = "xxx" }) - .ToSql(); -//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` -//FROM `tb_topic` a -//LEFT JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = ?bname +fsql.Select() + .LeftJoin("Category b on b.Id = a.CategoryId and b.Name = ?bname", new { bname = "xxx" }) + .ToList(); +//SELECT a.`Id`, a.`Title`, a.`Clicks`, a.`CreateTime`, a.`CategoryId` +//FROM `Topic` a +//LEFT JOIN Category b on b.Id = a.CategoryId and b.Name = ?bname ``` -## 5、子表Exists +延申问题:SQL联表如何 b 表的字段如果在 ToList 中指定? + ```csharp -var list2 = fsql.Select() +.ToList(a => new +{ + bid = Convert.ToInt32("b.Id"), + bName = "b.Name" +}) +``` + +## 4、子表Exists +```csharp +fsql.Select() .Where(a => fsql.Select().As("b").Where(b => b.Id == a.Id).Any()) .ToList(); -// SELECT a.`Id`, a.`TypeGuid`, a.`Title`, a.`CreateTime` -// FROM `xxx` a -// WHERE (exists(SELECT 1 -// FROM `xxx` b -// WHERE (b.`Id` = a.`Id`))) +//SELECT a.`Id`, a.`Title`, a.`Clicks`, a.`CreateTime`, a.`CategoryId` +//FROM `Topic` a +//WHERE (exists(SELECT 1 +// FROM `Topic` b +// WHERE (b.`Id` = a.`Id`) +// limit 0,1)) ``` > 提示:由于子查询的实体类与上层相同,使用 As("b") 指明别名,以便区分 -## 6、子表In +## 5、子表In ```csharp -var list2 = fsql.Select() +fsql.Select() .Where(a => fsql.Select().As("b").ToList(b => b.Id).Contains(a.Id)) .ToList(); -// SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` -// FROM `tb_topic` a -// WHERE (((cast(a.`Id` as char)) in (SELECT b.`Title` -// FROM `tb_topic` b))) +//SELECT a.`Id`, a.`Title`, a.`Clicks`, a.`CreateTime`, a.`CategoryId` +//FROM `Topic` a +//WHERE (((a.`Id`) in (SELECT b.`Id` +// FROM `Topic` b))) ``` -## 7、子表First/Count/Sum/Max/Min/Avg +## 6、子表First/Count/Sum/Max/Min/Avg ```csharp -var subquery = fsql.Select().ToSql(a => new +fsql.Select().ToList(a => new { all = a, - first = fsql.Select().Where(b => b.ParentId == a.Id).First(b => b.Id), - count = fsql.Select().Where(b => b.ParentId == a.Id).Count(), - sum = fsql.Select().Where(b => b.ParentId == a.Id).Sum(b => b.Score), - max = fsql.Select().Where(b => b.ParentId == a.Id).Max(b => b.Score), - min = fsql.Select().Where(b => b.ParentId == a.Id).Min(b => b.Score), - avg = fsql.Select().Where(b => b.ParentId == a.Id).Avg(b => b.Score) + first = fsql.Select().Where(b => b.CategoryId == a.Id).First(b => b.Id), + count = fsql.Select().Where(b => b.CategoryId == a.Id).Count(), + sum = fsql.Select().Where(b => b.CategoryId == a.Id).Sum(b => b.Clicks), + max = fsql.Select().Where(b => b.CategoryId == a.Id).Max(b => b.Clicks), + min = fsql.Select().Where(b => b.CategoryId == a.Id).Min(b => b.Clicks), + avg = fsql.Select().Where(b => b.CategoryId == a.Id).Avg(b => b.Clicks) }); ``` -## 8、AsSelect +## 7、AsSelect ```csharp -fsql.Select() +fsql.Select() .Where(a => a.Topics.AsSelect().Any(b => b.Title.Contains("xx"))) - .ToSql(); + .ToList(); ``` 效果等同于: ```csharp -fsql.Select() +fsql.Select() .Where(a => fsql.Select().Any(b => b.Title.Contains("xx"))) - .ToSql(); + .ToList(); ``` 将集合属性快速转换为 ISelect 进行子查询操作。 -## 9、WhereCascade +## 8、WhereCascade 多表查询时,像isdeleted每个表都给条件,挺麻烦的。WhereCascade使用后生成sql时,所有表都附上这个条件。 diff --git a/返回数据.md b/返回数据.md index 36ba4b7..38139f9 100644 --- a/返回数据.md +++ b/返回数据.md @@ -64,13 +64,15 @@ List t9 = fsql.Ado.Query("select * from song"); > 注意:Ado.Query 的实体特性是无效的,比如 [Column(Name = "xxx")] 无效 -## 6、巧用AsTable +## 6、WithSql ```csharp -var sql = fsql.Select() - .AsTable((a, b) => "(select * from tb_topic where clicks > 10)") - .Page(1, 10).ToList() +fsql.Select() + .WithSql("select * from Topic where clicks > 10") + .Page(1, 10) + .ToList() +//SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime` +//FROM (select * from Topic where clicks > 10) a ``` -> v1.0.1 ISelect 增加 WithSql("select * from user ...") 功能快速代理此方法 ## 7、ToChunk diff --git a/骚操作.md b/骚操作.md index 418acd0..1df0840 100644 --- a/骚操作.md +++ b/骚操作.md @@ -35,18 +35,16 @@ repo.InsertOrUpdate(实体); --- -# 4、巧用AsTable - +## 4、WithSql ```csharp -var sql = fsql.Select() - .AsTable((a, b) => "(select * from user where clicks > 10)") - .Page(1, 10).ToList() +fsql.Select() + .WithSql("select * from Topic where clicks > 10") + .Page(1, 10) + .ToList() +//SELECT a.`Id`, a.`Clicks`, a.`CategoryId`, a.`Title`, a.`CreateTime` +//FROM (select * from Topic where clicks > 10) a ``` -> 请注意 AsTable 里面的 SQL 两侧的括号 - -> v1.0.1 ISelect 增加 WithSql("select * from user ...") 功能快速代理此方法 - --- # 5、你不知道的,指定字段返回