【发布时间】:2011-06-29 15:35:55
【问题描述】:
我在 MySQL 数据库中有三个表
表 1 有以下字段
Name of Table: SupervisorTable
supervisorid (autoinc, bigint)
employeeid (bigint) //same as User.userid
supervisorname
Name of Table: User
userid (autoinc, bigint)
userFullName (string)
supervisorid (bigint)
divisionid (bigint)
Name of Table: DivisionTable
divisionid(autoinc, bigint)
divisionname (string)
我希望能够确定主管在哪个部门工作,我的输出应该是 主管姓名和部门名称。我最近的尝试:
SELECT supervisortable.supervisorname,
divisiontable.divisionname
FROM supervisortable,
user,
divisiontable
WHERE supervisortable.employeeid = user.userid;
这给出了主管姓名与每个部门的每个组合:
supervisorname divisionname
----------------------------
UserA Department1
UserB Department1
UserA Department2
...
UserB DepartmentN
【问题讨论】:
-
@Cocoa Dev:你试过什么?
-
到目前为止你有什么?看起来你有很多。和一个重复的字段。每个主管也是用户吗?
-
主管是否属于与其用户记录关联的部门?或者主管是否分配到其直接下属所在的部门?
-
每个主管也是用户。 SupervisorTable.employeeid 与 User.userid 的编号相同(autoinc、bigint、primary)
-
好的。那么 SupervisorTable.supervisorname 呢?和 userFullName 一样吗?