3个表join on的sql语句是什么
我有3个表,想把他们join到一起,怎么有重复的列
以下是我的代码:
select prjcourse.id1 AS 项目ID,prjinfo.prjname AS 项目名,prjcourse.id2 AS 阶段ID,prjphase.phasename AS 阶段名,
prjcourse.dttime AS 阶段所用时间 from prjcourse
JOIN prjinfo
on prjcourse.id1=prjinfo.prjid
JOIN prjphase
on prjcourse.id2=prjphase.phaseid
3个表分别是
prjcourse:id1,id2,id3,time
prjinfo:prjid,prjname
prjphase:phaseid,phasename
结果怎么出现了重复的:
项目id 项目名 阶段id 阶段名 时间
1 项目1 1 项目1阶段1 10
1 项目1 1 项目2阶段1 10
2 项目2 2 项目1阶段1 10
2 项目2 2 项目2阶段2 10
。。。。。。。
[解决办法]
试试用子查询:
select a.id1 AS 项目ID,b.prjname AS 项目名,a.id2 AS 阶段ID,c.phasename AS 阶段名,
a.time AS 阶段所用时间
from prjcourse a,prjinfo b,prjphase c
where a.id1=b.prjid and b.id2=c.phaseid
[解决办法]
select prjcourse.id1 AS 项目ID,prjinfo.prjname AS 项目名,prjcourse.id2 AS 阶段ID,prjphase.phasename AS 阶段名,
prjcourse.dttime AS 阶段所用时间 from prjinfo inner join prjcourse inner join prjphase on prjcourse.id2=prjphase.phaseid on prjcourse.id1=prjinfo.prjid
如果prjionfo有可能有空的情况,就用左联结或右联结可以解决问题