FreeSql (十九)多表查询
多表查詢,常用的有聯(lián)表 LeftJoin/InnerJoin/RightJoin ,這三個方法在上篇文章已經(jīng)介紹過。
除了聯(lián)表,還有子查詢 Where Exists,和 Select 子表:
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();[Table(Name = "tb_topic")] class Topic {[Column(IsIdentity = true, IsPrimary = true)]public int Id { get; set; }public int Clicks { get; set; }public int TestTypeInfoGuid { get; set; }public string Title { get; set; }public DateTime CreateTime { get; set; } }子表 Exists
var sql2222 = fsql.Select<Topic>().Where(a => fsql.Select<Topic>().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`)))//兩級相同的子表查詢 sql2222 = fsql.Select<Topic>().Where(a =>fsql.Select<Topic>().Where(b => b.Id == a.Id && fsql.Select<Topic>().Where(c => c.Id == b.Id).Where(d => d.Id == a.Id).Where(e => e.Id == b.Id).Offset(a.Id).Any()).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` AND exists(SELECT 1 // FROM `xxx` c // WHERE (c.`Id` = b.`Id`) AND (c.`Id` = a.`Id`) AND (c.`Id` = b.`Id`) // limit 0,1)) // limit 0,1))子表 First/Count/Sum/Max/Min/Avg
var subquery = fsql.Select<Topic>().ToSql(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) });系列文章導(dǎo)航
(一)入門
(二)自動遷移實(shí)體
(三)實(shí)體特性
(四)實(shí)體特性 Fluent Api
(五)插入數(shù)據(jù)
(六)批量插入數(shù)據(jù)
(七)插入數(shù)據(jù)時忽略列
(八)插入數(shù)據(jù)時指定列
(九)刪除數(shù)據(jù)
(十)更新數(shù)據(jù)
(十一)更新數(shù)據(jù) Where
(十二)更新數(shù)據(jù)時指定列
(十三)更新數(shù)據(jù)時忽略列
(十四)批量更新數(shù)據(jù)
(十五)查詢數(shù)據(jù)
(十六)分頁查詢
(十七)聯(lián)表查詢
(十八)導(dǎo)航屬性
(十九)多表查詢
(二十)多表查詢 WhereCascade
(二十一)查詢返回數(shù)據(jù)
(二十二)Dto 映射查詢
(二十三)分組、聚合
(二十四)Linq To Sql 語法使用介紹
(二十五)延時加載
(二十六)貪婪加載 Include、IncludeMany、Dto、ToList
(二十七)將已寫好的 SQL 語句,與實(shí)體類映射進(jìn)行二次查詢
(二十八)事務(wù)
(二十九)Lambda 表達(dá)式
(三十)讀寫分離
(三十一)分區(qū)分表
(三十二)Aop
(三十三)CodeFirst 類型映射
(三十四)CodeFirst 遷移說明
(三十五)CodeFirst 自定義特性
轉(zhuǎn)載于:https://www.cnblogs.com/FreeSql/p/11531362.html
總結(jié)
以上是生活随笔為你收集整理的FreeSql (十九)多表查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【深入JavaScript】3.Java
- 下一篇: 那些花儿