首页 > 其他分享 >联表查询

联表查询

时间:2022-08-24 13:58:40浏览次数:77  
标签:Name od 查询 联表 CustomId Id Select Left

用到的几个实体类

使用 CodeFirst 创建表

public class Custom
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        
        public string Name { get; set; }
    }
    
    
public class Order
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        
        public string Name { get; set; }
        
        public decimal Price { get; set; }
        
        [SugarColumn(IsNullable = true)]
        
        public DateTime CreateTime { get; set; }
        
        [SugarColumn(IsNullable = true)]
        public int CustomId { get; set; }
    }
    

public class OrderDetail
    {
        [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int ItemId { get; set; }
        
        public int OrderId { get; set; }
        
        public decimal? Price { get; set; }
        
        [SqlSugar.SugarColumn(IsNullable = true)]
        public DateTime? CreateTime { get; set; }
        
        [SugarColumn(IsIgnore = true)]
        public Order Order { get; set; }
    }
    
    
public class ViewOrder : Order
    {
        public string CustomName { get; set; }
    }

1.Join用法

1.1 语法糖1

优点:好理解,五个表以内的联表比较推荐

1.1.1 表和表的连接

List<ViewOrder> list = db.Queryable<Order>()
                .LeftJoin<Custom>((o, cus) => o.CustomId == cus.Id) //若是多个条件 用 &&
                .LeftJoin<OrderDetail>((o, cus, oritem) => o.Id == oritem.OrderId)
                 .Where((o, cus, oritem) => o.Id == 4 && oritem.Price == 50)
                .Select((o, cus) => new ViewOrder { Id = o.Id, CustomName =cus.Name })
                .ToList();
                
            //    SELECT  `o`.`Id` AS `Id` , `cus`.`Name` AS `CustomName`  FROM `Order` o
            //    Left JOIN `Custom` cus ON( `o`.`CustomId` = `cus`.`Id` )  
            //    Left JOIN `OrderDetail` oritem ON( `o`.`Id` = `oritem`.`OrderId` )   
            //    WHERE( `o`.`Id` = '4')

若将 .Select((o, cus) => new ViewOrder { Id = o.Id, CustomName =cus.Name }) 修改成.Select((o, cus) => new ViewOrder()) 那就是查询所有字段

1.1.2 表和Queryable的连接 (主表左连了一个由子查询组合的表)

var rightQueryable = db.Queryable<Custom>()
                .LeftJoin<OrderDetail>((c, o) => c.Id == o.ItemId)
                .Select(c => c);

var list = db.Queryable<Order>()
               .LeftJoin(rightQueryable, (o, r) => o.CustomId == r.Id)
               .Where(o=>o.Id==4)
               .Select(o => o)
               .ToList();


//SELECT o.* FROM `Order` o 
//Left JOIN(SELECT c.*FROM `Custom` c Left JOIN `OrderDetail` o ON( `c`.`Id` = `o`.`ItemId` )   ) r ON ( `o`.`CustomId` = `r`.`Id` )
//WHERE( `o`.`Id` = '4')

1.1.3 Queryable和表的连接 (以一个子查询为主表关联其他表)

var queryable = db.Queryable<Order>();

var list = db.Queryable(queryable)
            .LeftJoin<Custom>((o, c) => o.CustomId == c.Id)
            .LeftJoin<OrderDetail>((o, c, od) => o.Id == od.OrderId).Select(o => o)
            .Where(o => o.Id == 4)
            .ToList();
            
//SELECT o.* FROM  
//(SELECT * FROM  (SELECT `Id`,`Name`,`Price`,`CreateTime`,`CustomId` FROM `Order`  ) t ) o 
//Left JOIN `Custom` c ON ( `o`.`CustomId` = `c`.`Id` )  
//Left JOIN `OrderDetail` od ON ( `o`.`Id` = `od`.`OrderId` )   
//WHERE ( `o`.`Id` = '4' )

1.2 语法糖2

  • 优点:五个表以上用这种方式比较快捷
  • 缺点:不能套子查询( leftjoin(queryable) )
var list = db.Queryable<Order, Custom, OrderDetail>((o, c, od) => new JoinQueryInfos(
            JoinType.Left, o.CustomId == c.Id,
            JoinType.Left, o.Id == od.OrderId
            ))
            .Select((o, c, od) => new { Id = o.Id, Name = o.Name, Cname = c.Name, Price = od.Price, CreateTime = od.CreateTime})
            .ToList();

//SELECT  `o`.`Id` AS `Id` , `o`.`Name` AS `Name` , `c`.`Name` AS `Cname` , `od`.`Price` AS `Price` , `od`.`CreateTime` AS `CreateTime`  FROM `Order` o
//Left JOIN `Custom` c ON( `o`.`CustomId` = `c`.`Id` )  
//Left JOIN `OrderDetail` od ON( `o`.`Id` = `od`.`OrderId` )

1.3 语法糖3

  • 'inner join 可以用这种方式'
 var list = db.Queryable<Order, Custom, OrderDetail>((o, c, od) => o.CustomId == c.Id && o.Id == od.OrderId)
               .Select((o, c, od) => new { Id = o.Id, Name = o.Name, Cname = c.Name, Price = od.Price, CreateTime = od.CreateTime })
               .ToList();
               
//SELECT  `o`.`Id` AS `Id` , `o`.`Name` AS `Name` , `c`.`Name` AS `Cname` , `od`.`Price` AS `Price` , `od`.`CreateTime` AS `CreateTime`  
//FROM `Order` o  ,`Custom`  c ,`OrderDetail`  od
//WHERE(( `o`.`CustomId` = `c`.`Id` ) AND( `o`.`Id` = `od`.`OrderId` ))

2.Where用法

注意:写在 Select() 之前

.Where(o => o.Id == 4)
.Where((o, cus, oritem) => o.Id == 4 && oritem.Price == 50)

3.OrderBy用法

注意:写在 Select() 之前

.OrderBy(st => st.StudentId) 
.OrderBy((st, sc) => sc.SchoolId, OrderByType.Desc)

4.GroupBy用法

注意:写在 Select() 之前

.GroupBy(it => it.Name)
.GroupBy((st, sc) => sc.SchoolId)

5.Select用法

连表必须加上 Select 不然会查询出重复列而报错,一般 Select() 写在 ToList() 之前

基础用法

//新类
.Select((o,i)=>new 类名{Id=o.Id,Name=o.Name,SchoolName=i.Name}).ToList();

//匿名对象
.Select((o,i)=>new {Id=o.Id,Name=o.Name,SchoolName=i.Name}).ToList();

5.1 返回匿名对象

5.1.1 一个一个字段赋值

参考 "1.2 语法糖2" 中 Select的写法

5.1.2 自动主表赋值 表.*
  • o.Id是表的任意一个属性, SelectAll是查询全部 o.*
  • .SelectAll等同于SqlFunc.GetSelfAndAutoFill是个语法糖
 var list = db.Queryable<Order, Custom, OrderDetail>((o, c, od) => new 
        JoinQueryInfos(
                JoinType.Left, o.CustomId == c.Id,
                JoinType.Left, o.Id == od.OrderId
                ))
                 .Select((o, c, od) => new { 
                 d = o.Id.SelectAll(), 
                 CName = c.Name, 
                 Time = od.CreateTime })
                .ToList();
                
//SELECT o.*, `c`.`Name` AS `CName` , `od`.`CreateTime` AS `Time`  FROM `Order` o
//Left JOIN `Custom` c ON ( `o`.`CustomId` = `c`.`Id` )
//Left JOIN `OrderDetail` od ON ( `o`.`Id` = `od`.`OrderId` )

5.2 返回到新类

参考 "1.1.1 表和表的连接" 中 Select的写法

5.3 自动映射机制

Select用的是自动填充这样使用方便,高并发的地方还是写成上面那种方式

var list = db.Queryable<Order, Custom, OrderDetail>((o, c, od) => new 
        JoinQueryInfos(
                JoinType.Left, o.CustomId == c.Id,
                JoinType.Left, o.Id == od.OrderId
                ))
                 .Select<ViewOrder>()
                .ToList();

//SELECT c.`Name` AS `CustomName`,o.`Id` AS `Id`,o.`Name` AS `Name`,o.`Price` AS `Price`,o.`CreateTime` AS `CreateTime`,o.`CustomId` AS `CustomId` 
//FROM `Order` o
//Left JOIN `Custom` c ON( `o`.`CustomId` = `c`.`Id` )  
//Left JOIN `OrderDetail` od ON( `o`.`Id` = `od`.`OrderId` )

5.4 更多用法

6.Full Join用法

文档参考:联表查询

标签:Name,od,查询,联表,CustomId,Id,Select,Left
From: https://www.cnblogs.com/DotNeter-Hpf/p/16619624.html

相关文章

  • 导航查询
    前言导航查询特点:主要处理主对象里面有子对象这种层级关系查询只需要配置特性和主键即可,不需要外键1.导航查询用法 (关键字:Includes)1.1一对一用到的实体类:publi......
  • linux查询进程被kill的日志 oom killer
    OOMKilledLinux内核有个机制叫OOMkiller(OutOfMemorykiller),该机制会监控那些占用内存过大,尤其是瞬间占用内存很快的进程,然后防止内存耗尽而自动把该进程杀掉,内核检......
  • django中聚合函数查询和分组聚合查询
    聚合函数:Max,Min,Count首字母都要大写,且后面的参数加‘’号,不然会报错,还有就是,如果是Count(')的话,需要加个别名,比如(m=Count('')),不然会报错,所以为了记住,我们平时MaxMin的......
  • 同样的代码逻辑,同样的表结构,由于数据量的不同导致查询没有走索引,而引起请求超时案例分
    问题描述:预发环境目前加载全部变更会超时,线上正常。需要看一下是什么问题避免预发上的问题带到线上      分析解决过程:EXPLAINSELECT i.damand_manager......
  • nebula 查询节点数、边数
    SHOWSTATS-NebulaGraphDatabase手册(nebula-graph.com.cn)1#选择图空间。2nebula>USEbasketballplayer;34#执行SUBMITJOBSTATS。5nebula>SU......
  • springboot+mybatis-plus-join+mysql实现连表查询
    1.简介  Mybatis是目前比较主流的持久层框架,使用非常广泛。Mybatis-Plus是基于Mybatis增强工具包,越来越受到开发人员的喜爱。  在使用Mybatis-Plus开发时,简单的crud......
  • go 查询es数据神器
    可以通过关键字和时间间隔进行查询,关键字可以像kibana上的查询语句填写,代码搞起//搜索返回的内容typeResultstruct{ Messagestring`json:"message"` Sourcestr......
  • mysql知识总结-连接查询
    Join链接方式Leftjoin左边的表为左表,rightjoin右边的表为右表1、左链接1.1、是以左表为基础,根据ON后给出的两表的条件将两表连接起来。结果会将左表所有的查询信息......
  • postgresql从库查询被终止怎么办
    一,问题描述:PG流复制场景下,默认配置下,如果在PG从库执行长时间的查询,会出现查询的报错。提示ERROR:cancelingstatementduetoconflictwithrecoveryDETAIL:Userqu......
  • Mybatis下@Select注解下使用like模糊查询
    Mybatis下@Select注解使用模糊查询要使用concat方法拼接%百分号和关键词,案例如下:packagecom.xzit.mapper;importcom.xzit.entity.Emp;importorg.apache.ibatis.ann......