mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-16 05:10:55 +08:00
update
8
分页查询.md
8
分页查询.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; }
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
41
单表查询.md
41
单表查询.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<Topic>()
|
||||
fsql.Select<Topic>()
|
||||
.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<Topic>()
|
||||
fsql.Select<Topic>()
|
||||
.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<Topic>()
|
||||
fsql.Select<Topic>()
|
||||
.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<User>()
|
||||
.AsTable((a, b) => "(select * from user where clicks > 10)")
|
||||
fsql.Select<Topic>()
|
||||
.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 ...") 功能快速代理此方法
|
||||
|
||||
## 参考资料
|
||||
|
||||
|
||||
153
多表查询.md
153
多表查询.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<Topic> 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<Topic>()
|
||||
.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<Topic>()
|
||||
.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<Topic>()
|
||||
.LeftJoin<TestTypeInfo>((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<Topic>().From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||
.LeftJoin(a => a.TestTypeInfoGuid == b.Guid)
|
||||
.LeftJoin(a => b.ParentId == c.Id))
|
||||
.ToSql();
|
||||
fsql.Select<Topic, Category, CategoryType>()
|
||||
.LeftJoin((a,b,c) => a.CategoryId == b.Id)
|
||||
.LeftJoin((a,b,c) => b.ParentId == c.Id)
|
||||
.ToList();
|
||||
|
||||
//或者
|
||||
sql = fsql.Select<Topic, TestTypeInfo, TestTypeParentInfo>()
|
||||
.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<Topic>().From<Category, CategoryType>((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<Topic>()
|
||||
.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<Topic>()
|
||||
.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<Topic>()
|
||||
.ToList(a => new
|
||||
{
|
||||
bid = Convert.ToInt32("b.Id"),
|
||||
bName = "b.Name"
|
||||
})
|
||||
```
|
||||
|
||||
## 4、子表Exists
|
||||
```csharp
|
||||
fsql.Select<Topic>()
|
||||
.Where(a => fsql.Select<Topic>().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<Topic>()
|
||||
fsql.Select<Topic>()
|
||||
.Where(a => fsql.Select<Topic>().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<Topic>().ToSql(a => new
|
||||
fsql.Select<Category>().ToList(a => new
|
||||
{
|
||||
all = a,
|
||||
first = fsql.Select<Child>().Where(b => b.ParentId == a.Id).First(b => b.Id),
|
||||
count = fsql.Select<Child>().Where(b => b.ParentId == a.Id).Count(),
|
||||
sum = fsql.Select<Child>().Where(b => b.ParentId == a.Id).Sum(b => b.Score),
|
||||
max = fsql.Select<Child>().Where(b => b.ParentId == a.Id).Max(b => b.Score),
|
||||
min = fsql.Select<Child>().Where(b => b.ParentId == a.Id).Min(b => b.Score),
|
||||
avg = fsql.Select<Child>().Where(b => b.ParentId == a.Id).Avg(b => b.Score)
|
||||
first = fsql.Select<Topic>().Where(b => b.CategoryId == a.Id).First(b => b.Id),
|
||||
count = fsql.Select<Topic>().Where(b => b.CategoryId == a.Id).Count(),
|
||||
sum = fsql.Select<Topic>().Where(b => b.CategoryId == a.Id).Sum(b => b.Clicks),
|
||||
max = fsql.Select<Topic>().Where(b => b.CategoryId == a.Id).Max(b => b.Clicks),
|
||||
min = fsql.Select<Topic>().Where(b => b.CategoryId == a.Id).Min(b => b.Clicks),
|
||||
avg = fsql.Select<Topic>().Where(b => b.CategoryId == a.Id).Avg(b => b.Clicks)
|
||||
});
|
||||
```
|
||||
|
||||
## 8、AsSelect
|
||||
## 7、AsSelect
|
||||
|
||||
```csharp
|
||||
fsql.Select<TestTypeInfo>()
|
||||
fsql.Select<Category>()
|
||||
.Where(a => a.Topics.AsSelect().Any(b => b.Title.Contains("xx")))
|
||||
.ToSql();
|
||||
.ToList();
|
||||
```
|
||||
|
||||
效果等同于:
|
||||
|
||||
```csharp
|
||||
fsql.Select<TestTypeInfo>()
|
||||
fsql.Select<Category>()
|
||||
.Where(a => fsql.Select<Topic>().Any(b => b.Title.Contains("xx")))
|
||||
.ToSql();
|
||||
.ToList();
|
||||
```
|
||||
|
||||
将集合属性快速转换为 ISelect 进行子查询操作。
|
||||
|
||||
## 9、WhereCascade
|
||||
## 8、WhereCascade
|
||||
|
||||
多表查询时,像isdeleted每个表都给条件,挺麻烦的。WhereCascade使用后生成sql时,所有表都附上这个条件。
|
||||
|
||||
|
||||
12
返回数据.md
12
返回数据.md
@@ -64,13 +64,15 @@ List<dynamic> t9 = fsql.Ado.Query<dynamic>("select * from song");
|
||||
|
||||
> 注意:Ado.Query 的实体特性是无效的,比如 [Column(Name = "xxx")] 无效
|
||||
|
||||
## 6、巧用AsTable
|
||||
## 6、WithSql
|
||||
```csharp
|
||||
var sql = fsql.Select<User>()
|
||||
.AsTable((a, b) => "(select * from tb_topic where clicks > 10)")
|
||||
.Page(1, 10).ToList()
|
||||
fsql.Select<Topic>()
|
||||
.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
|
||||
|
||||
|
||||
16
骚操作.md
16
骚操作.md
@@ -35,18 +35,16 @@ repo.InsertOrUpdate(实体);
|
||||
|
||||
---
|
||||
|
||||
# 4、巧用AsTable
|
||||
|
||||
## 4、WithSql
|
||||
```csharp
|
||||
var sql = fsql.Select<User>()
|
||||
.AsTable((a, b) => "(select * from user where clicks > 10)")
|
||||
.Page(1, 10).ToList()
|
||||
fsql.Select<Topic>()
|
||||
.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、你不知道的,指定字段返回
|
||||
|
||||
Reference in New Issue
Block a user