1、创建三张测试表;
createtabletest_a(aidint,anamevarchar(20))
createtabletest_b(bidint,bnamevarchar(20))
createtabletest_c(aidint,bidint,valuevarchar(20))
2、三张表中分别插入数据;
insertintotest_avalues(1,'aname1')
insertintotest_bvalues(2,'bname1')
insertintotest_cvalues(1,2,'cvalue')
3、查询表中记录;
select10,a.*fromtest_aa
unionall
select20,b.*fromtest_bb
unionall
select*fromtest_cc
4、编写sql,进行三表关联;
selecta.aname,b.bname,c.value
fromtest_ccjointest_aa
onc.aid=a.aid
jointest_bb
onc.bid=b.bid
SELECTmain.dept_name as 部门名称,
count(*) as 在职员工数,
sum(acu_salary) as 实发工资总额
FROM
dept main
INNER JOIN emp a ON main.dept_code = a.dept_code
INNER JOIN salary b ON a.emp_id = b.emp_id
WHERE
-- 1为有效标识
main.alive_flag = '1'
AND a.alive_flag = '1'
AND b.alive_flag = '1'
AND b.`month` = '201101'
GROUP BY
a.dept_code
HAVING
count(*) >5
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)