mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-04 07:20:51 +08:00
update
51
多表查询.md
51
多表查询.md
@@ -28,7 +28,7 @@ class TestTypeParentInfo {
|
||||
ISelect<Topic> select => fsql.Select<Topic>();
|
||||
```
|
||||
|
||||
### 利用导航属性联表
|
||||
## 1、(联表)利用导航属性
|
||||
```csharp
|
||||
sql = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid).ToSql();
|
||||
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime`
|
||||
@@ -44,23 +44,17 @@ sql = select
|
||||
//LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId`
|
||||
```
|
||||
|
||||
### 没有导航属性联表
|
||||
> 提示:正确配置[导航关系](https://github.com/2881099/FreeSql/wiki/%e5%ae%9e%e4%bd%93%e5%85%b3%e7%b3%bb)后,不再需要手工调用 LeftJoin
|
||||
|
||||
## 2、普通联表
|
||||
```csharp
|
||||
sql = select.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`
|
||||
|
||||
sql = select
|
||||
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid)
|
||||
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).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`
|
||||
//LEFT JOIN `TestTypeParentInfo` c ON c.`Id` = b.`ParentId`
|
||||
```
|
||||
|
||||
### 联表任意查
|
||||
## 3、任意联表
|
||||
```csharp
|
||||
sql = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||
.LeftJoin(a => a.TestTypeInfoGuid == b.Guid)
|
||||
@@ -71,7 +65,14 @@ sql = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||
//LEFT JOIN `TestTypeParentInfo` c ON b.`ParentId` = c.`Id`
|
||||
```
|
||||
|
||||
### 原生SQL联表
|
||||
或者
|
||||
```csharp
|
||||
sql = fsql.Select<Topic, TestTypeInfo, TestTypeParentInfo>()
|
||||
.LeftJoin((a,b,c) => a.TestTypeInfoGuid == b.Guid)
|
||||
.LeftJoin((a,b,c) => b.ParentId == c.id).ToSql();
|
||||
```
|
||||
|
||||
## 4、SQL联表
|
||||
```csharp
|
||||
sql = 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`
|
||||
@@ -79,33 +80,23 @@ sql = select.LeftJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name
|
||||
//LEFT JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = ?bname
|
||||
```
|
||||
|
||||
## 多表,利用导航属性
|
||||
## 5、利用导航属性,查询条件
|
||||
```csharp
|
||||
sql = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TestTypeInfoGuid).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, `TestTypeInfo` a__Type
|
||||
//WHERE (a__Type.`Name` = 'typeTitle' AND a__Type.`Guid` = a.`TestTypeInfoGuid`)
|
||||
|
||||
sql = select.Where(a => a.Type.Parent.Name == "tparent").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, `TestTypeInfo` a__Type, `TestTypeParentInfo` a__Type__Parent
|
||||
//WHERE (a__Type__Parent.`Name` = 'tparent')
|
||||
```
|
||||
|
||||
## 多表,没有导航属性
|
||||
## 6、没有导航属性,查询条件
|
||||
```csharp
|
||||
sql = select.Where<TestTypeInfo>((a, b) => b.Guid == a.TestTypeInfoGuid && b.Name == "typeTitle").ToSql();
|
||||
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime`
|
||||
//FROM `tb_topic` a, `TestTypeInfo` b
|
||||
//WHERE (b.`Guid` = a.`TestTypeInfoGuid` AND b.`Name` = 'typeTitle')
|
||||
|
||||
sql = select.Where<TestTypeInfo, TestTypeParentInfo>((a, b, c) => c.Name == "tparent").ToSql();
|
||||
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime`
|
||||
//FROM `tb_topic` a, `TestTypeParentInfo` c
|
||||
//WHERE (c.`Name` = 'tparent')
|
||||
```
|
||||
|
||||
## 多表,任意查
|
||||
## 7、任意条件
|
||||
```csharp
|
||||
sql = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||||
.Where(a => a.Id == 10 && c.Name == "xxx")
|
||||
@@ -124,7 +115,7 @@ sql = fsql.Select<Topic, TestTypeInfo, TestTypeParentInfo>()
|
||||
.ToSql();
|
||||
```
|
||||
|
||||
## 子表 Exists
|
||||
## 8、子表 Exists
|
||||
```csharp
|
||||
var list2 = select.Where(a => select.Where(b => b.Id == a.Id).Any()).ToList();
|
||||
// SELECT a.`Id`, a.`TypeGuid`, a.`Title`, a.`CreateTime`
|
||||
@@ -134,7 +125,7 @@ var list2 = select.Where(a => select.Where(b => b.Id == a.Id).Any()).ToList();
|
||||
// WHERE (b.`Id` = a.`Id`)))
|
||||
```
|
||||
|
||||
## 子表 In
|
||||
## 9、子表 In
|
||||
|
||||
```csharp
|
||||
var list2 = select.Where(a => select.ToList(b => b.Id).Contains(a.Id)).ToList();
|
||||
@@ -144,7 +135,7 @@ var list2 = select.Where(a => select.ToList(b => b.Id).Contains(a.Id)).ToList();
|
||||
// FROM `tb_topic` b)))
|
||||
```
|
||||
|
||||
## 子表 First/Count/Sum/Max/Min/Avg
|
||||
## 10、子表 First/Count/Sum/Max/Min/Avg
|
||||
```csharp
|
||||
var subquery = select.ToSql(a => new {
|
||||
all = a,
|
||||
@@ -157,7 +148,7 @@ var subquery = select.ToSql(a => new {
|
||||
});
|
||||
```
|
||||
|
||||
## AsSelect
|
||||
## 11、AsSelect
|
||||
|
||||
```csharp
|
||||
fsql.Select<TestTypeInfo>()
|
||||
@@ -175,7 +166,7 @@ fsql.Select<TestTypeInfo>()
|
||||
|
||||
将集合属性快速转换为 ISelect 进行子查询操作。
|
||||
|
||||
## WhereCascade
|
||||
## 12、WhereCascade
|
||||
|
||||
多表查询时,像isdeleted每个表都给条件,挺麻烦的。WhereCascade使用后生成sql时,所有表都附上这个条件。
|
||||
|
||||
|
||||
5
更新日志.md
5
更新日志.md
@@ -1,6 +1,11 @@
|
||||
|
||||
完整版本:年数-月-日-当日版本号,FreeSql、FreeSql.Repository、FreeSql.DbContext 版本号相同。
|
||||
|
||||
## v1.0.0(预告)
|
||||
|
||||
- 优化 AsTable 分表查询 Any/Min/Max/Avg/Sum/Count 的处理;
|
||||
- 调整 Avg 方法返回值为 double,Sum 方法返回值为 decimal;
|
||||
|
||||
## v0.12.21
|
||||
|
||||
- 增加 Where In 表达式解析;[wiki](https://github.com/2881099/FreeSql/wiki/%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2#%E5%AD%90%E8%A1%A8-exists)
|
||||
|
||||
Reference in New Issue
Block a user