数据库多表联查(左连接查询)

现有三张数据表如下:

ApplyProcessModule(模块表)
模块表
ApplyProcessTemplate(模板表)
模板表
ApplyProcessStepConfigInfo(流程步骤表)
流程步骤表

表关系如下:
流程步骤表的流程模板Id=模板表模板id
流程步骤表的模块id=模块表模块id
流程步骤表的下一步骤ID=模块表模块id

数据库代码如下:
select *from(
select row_number() over (order by A.id asc) as num,
      a.id,
      b.Name as a,
      d.Name as c,
      a.NextStepMethod,
      h.Name as b,
      a.NextStepConfig,
      a.StepType,
      a.StepConfigDescription,
      a.StepConfigModuleJson,
      a.StepConfigSort,
      a.NoticeMessage,
      a.Addtime,
      a.ModifyTime,
      a.CreateBy,
      a.ModifyBy
  From ApplyProcessStepConfigInfo a WITH(NOLOCK) 
  left join ApplyProcessTemplate b ON a.TemplateId=b.Id
  left join ApplyProcessModule d ON a.ModuleId=d.Id
  left join ApplyProcessModule h ON a.NextStepId=h.Id
  ) a
  where num>0 and num<=20 order by num
 
  

MVC中查询代码块
public DataSet GetAll(int? page=null,int? row=null,string sort=null,string order=null)
{
   //连接SQL
  var sql_joinlist=new List<SQLJoin>()
     {
       new SQLJoin("b",@"left join ApplyProcessTemplate b ON a.TemplateId=b.Id "),
       new SQLJoin("d",@"left join ApplyProcessModule d ON a.ModuleId=b.Id "),
       new SQLJoin("h",@"left join ApplyProcessModule h ON a.NextStepId=b.Id "),
     }
   //条件过滤SQL
      var sb_sql_r1=new SQLBuilder(@"
      a.id,
      b.Name as a,
      d.Name as c,
      a.NextStepMethod,
      h.Name as b,
      a.NextStepConfig,
      a.StepType,
      a.StepConfigDescription,
      a.StepConfigModuleJson,
      a.StepConfigSort,
      a.NoticeMessage,
      a.Addtime,
      a.ModifyTime,
      a.CreateBy,
      a.ModifyBy
     From ApplyProcessStepConfigInfo a WITH(NOLOCK)",sql_footer:@"",sql_joinlist:sql_joinlist,keys:"b,d,h");
     //字段查询SQL
     var sb_sql_r2=new SQLBuilder(@"
     select 
      a.Id,
      b.Name as a,
      d.Name as c,
      a.NextStepMethod,
      h.Name as b,
      a.NextStepConfig,
      a.StepType,
      a.StepConfigDescription,
      a.StepConfigModuleJson,
      a.StepConfigSort,
      a.NoticeMessage,
      a.Addtime,
      a.ModifyTime,
      a.CreateBy,
      a.ModifyBy
      From ApplyProcessStepConfigInfo a WITH(NOLOCK)",sql_footer:@"",sql_joinlist:sql_joinlist,keys:"b,d,h");
      //输出SQL
      if(String.ISNullOrEmpty(sort))
      {
         sort="Id";
      }
      if(String.ISNullOrEmpty(order))
      {
         order="acs";//升序 desc降序
      }
      StringBuilder sb_sql_r3=new StringBuilder(string.Fromat(@"select * from (select row_number() over (order by A.{0} {1} as num,{2}) a",sort,order,sb_sql_r1.ToString()));
      using (var dbContext=new DBContext())
      {
      if(!page.HasValue)
      {
      page=1;
      sb_sql_r3= new SQLBuilder(@"
      select 
       a.Id,
       b.Name as a,
       d.Name as c,
       a.NextStepMethod,
       h.Name as b,
       a.NextStepConfig,
       a.StepType,
       a.StepConfigDescription,
       a.StepConfigModuleJson,
       a.StepConfigSort,
       a.NoticeMessage,
       a.Addtime,
       a.ModifyTime,
       a.CreateBy,
       a.ModifyBy
      from ({0}) as result",sb_sql_r3.ToString()));
      }
      else
      {
      sb_sql_r3.Insert(0,string.Fromat(@"select count(*) from({0}) r2 ; ",sb_sql_r2));
      }
      if(!rows.HasValue)
      {
        rows=int.MaxValue;
      }
      sb_sql_r3.AppendFromat(" where num>{0} and num<={1} order by num",(page-1)*rows,page*rows)
      return dbContext.SqlDataSet(sb_sql_r3.ToString());
}
SQLJoin.cs
public string Key;
public string Value;
public bool IsAdd;

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值