通过获取其他表中的列来添加特定列

时间:2016-05-13 08:28:15

标签: mysql

我的问题是将这两个表合并为1,父表是tbl1。

tbl1
userid | Checkin | checkout

tbl2
userid | Name | Department 

我运行此操作,并出现错误

  

"模棱两可的字段列表"

SELECT userid, checkin, checkout
from tbl1
join tbl2
on tbl1.userid = tbl2.userid
order by tbl1.userid

我希望我的桌子像这样:

userid | Checkin | checkout | Name | Deparment

2 个答案:

答案 0 :(得分:1)

试试这个;)

SELECT tbl1.userid, checkin, checkout, Name, Department
from tbl1
join tbl2
on tbl1.userid = tbl2.userid
order by tbl1.userid

答案 1 :(得分:1)

两个表都有列userid,因此您必须指定表或表别名以及所选列。(tbl1.userid或tbl2.userid)

SELECT tbl1.userid, checkin, checkout, Name, Department 
from tbl1
join tbl2
on tbl1.userid = tbl2.userid
order by tbl1.userid