数据库三表连接查询怎么做

数据库三表连接查询怎么做,第1张

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

SELECT

main.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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10772093.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存