---
title: "mysql数据库中的多表联查"
author: "574965385"
date: "2010-12-19T09:32:46Z"
tags: ["C", "C#", "C++", "MySQL"]
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2010-12-19T09:32:46Z"
id: 846476
url: "https://574965385.iteye.com/blog/846476"
---

在项目中遇见了mysql库的多表联查，记录下来，供以后查阅。

select a.slzgh as Zgh,c.username as Xm,a.slksh as Ksh,d.Ksmc,a.Ffdate,b.Sortid,b.Sortname,sum(b.Silnum) as Silnum 
from offmate a 
inner join offmatedetail b on a.contentid=b.contentid 
left join user c on a.slzgh=c.userid 
left join ks d on a.slksh=d.ksh  group by a.slzgh			
where a.sflq=2 and b.sortid='"+sortid+"' and a.ffdate between '"+sdate+"' and '"+edate+"'"
前面两个表提供主要数据，后面两个表提供单项名称。
根据id号和日期区间来查询数据。