sql三表链接查询

Posted 新作伟平

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql三表链接查询相关的知识,希望对你有一定的参考价值。

  select * from [dbo].[Contract_BaseInfor] as x
  join [dbo].[MaterialType] as a on x.RowGuid = a.ContractRowGuid
  join [dbo].[EquipmentEFile_Category] as b on a.RowGuid = b.MaterialDetailRowGuid
  join [dbo].[EquipmentEFile] as c on c.ParentRowGuid = b.RowGuid
  where x.RowGuid=655b9e4f-4f75-4eeb-8598-de14834dd640 and a.Count >0 and c.IsMaxVersion=1
  order by c.StorageName

 dmrlist:合同列表

var contractMT = (from con in dmrlist
                                  join m in dc_ModelData.MaterialType.Where(p => p.MonomerProject == bidRowGuid && p.Count > 0) on con.RowGuid equals m.ContractRowGuid
                                  select new { ConName = con.Name, ConRowGuid = con.RowGuid, MTRowGuid = m.RowGuid } into jtemp
                                  group jtemp by new { jtemp.ConName, jtemp.ConRowGuid } into temp
                                  select new { ConName = temp.Key.ConName, ConRowGuid = temp.Key.ConRowGuid, MTCount = temp.Count() }).ToList();
                int contractMTCount = contractMT.Count;

                var contractEFile = (from con in contractMT
                                     join m in dc_ModelData.MaterialType.Where(p => p.MonomerProject == bidRowGuid && p.Count > 0) on con.ConRowGuid equals m.ContractRowGuid
                                     join h in dc_ModelData.EquipmentEFile_Category.Where(p => p.BidSectionRowGuid == bidRowGuid && p.Title == "族文件") on m.RowGuid equals h.MaterialDetailRowGuid
                                     join k in dc_ModelData.EquipmentEFile.Where(p => p.IsMaxVersion == true) on h.RowGuid equals k.ParentRowGuid
                                     select new { ConName = con.ConName, ConRowGuid = con.ConRowGuid, MTRowGuid = m.RowGuid, EFileRowGuid = k.RowGuid } into jtemp
                                     group jtemp by new { jtemp.ConName, jtemp.ConRowGuid } into temp
                                     select new { ConName = temp.Key.ConName, ConRowGuid = temp.Key.ConRowGuid, EFileCount = temp.Count() }).ToList();
                int contractEFileCount = contractEFile.Count;

 

以上是关于sql三表链接查询的主要内容,如果未能解决你的问题,请参考以下文章

sql多表链接之三表连接查询

SQl Server 表链接查询

SQL server基础知识(表操作数据约束多表链接查询)

SQL多表链接查询、嵌入SELECT语句的子查询技术

Mybatis多表链接查询重复字段问题

mysql数据查询之多表链接