把一个表中的1项和另一个表中很多项怎么关联?
表1(usrinfo):usrtxt usrname
1 我
2 你
3 他
表2(prjphase):usrid1 usrid2 usrid3
1 2 3
2 3 1
就是想把prjphase的3个值显示出对应的usrname
以下是错误的做法:
select usrinfo.usrname AS 参与者1,usrinfo.usrname AS 参与者2,usrinfo.usrname AS 参与者3
from prjphase
left join usrinfo
on usrinfo.usrtxt=prjphase.usrid1 and usrinfo.usrtxt=prjphase.usrid2 and usrinfo.usrtxt=prjphase.usrid3
------解决方案--------------------或者
select (select usrname from usrinfo where usrtxt=a.usrid1 ) 参与者1,
(select usrname from usrinfo where usrtxt=a.usrid2 ) 参与者2,
(select usrname from usrinfo where usrtxt=a.usrid3 ) 参与者3
from prjphase a
------解决方案--------------------我寫的那個應該沒有問題的。
select B.usrname AS 参与者1,C.usrname AS 参与者2,
from prjphase A
left join usrinfo B
on B.usrtxt=A.usrid1
left join usrinfo C
on C.usrtxt=A.usrid2
這個是有錯誤的,多了個 ", ".
應該為
select B.usrname AS 参与者1,C.usrname AS 参与者2
from prjphase A
left join usrinfo B
on B.usrtxt=A.usrid1
left join usrinfo C
on C.usrtxt=A.usrid2
三找的可以,但是效率沒有用關聯的高。