【问题标题】:MySQL query using INNER JOIN, Not returning what i'd expectMySQL查询使用INNER JOIN,不返回我所期望的
【发布时间】:2009-11-24 10:27:43
【问题描述】:

以下查询有问题:

SELECT costingjobtimes.TimeStamp, costingdepartment.DeptDesc,  costingemployee.Name, costingjobtimes.TimeElapsed FROM costingemployee INNER JOIN (costingdepartment INNER JOIN costingjobtimes ON costingdepartment.DeptID = costingjobtimes.DeptID) ON costingemployee.EmployeeID = costingjobtimes.EmployeeID;

我希望它返回 costingjobtimes 数据库中的每一行,但它目前只返回 4。

基本上,我有 3 张桌子。 (costingjobtimes, costingdepartment, costingemployee) 它们如下:

mysql> DESCRIBE costingemployee
    -> ;
+------------+------------+------+-----+---------+-------+
| Field      | Type       | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+-------+
| EmployeeID | varchar(8) |      | PRI |         |       |
| Name       | text       |      |     |         |       |
+------------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> DESCRIBE costingdepartment
    -> ;
+----------+------------------+------+-----+---------+-------+
| Field    | Type             | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| DeptID   | int(10) unsigned |      | PRI | 0       |       |
| DeptDesc | text             |      |     |         |       |
+----------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> DESCRIBE costingjobtimes
    -> ;
+-------------+------------------+------+-----+---------------------+-----------
-----+
| Field       | Type             | Null | Key | Default             | Extra
     |
+-------------+------------------+------+-----+---------------------+-----------
-----+
| id          | int(10) unsigned |      | PRI | NULL                | auto_incre
ment |
| EmployeeID  | text             |      |     |                     |
     |
| DeptID      | int(10) unsigned |      |     | 0                   |
     |
| JobNumber   | text             |      |     |                     |
     |
| TimeElapsed | decimal(10,5)    |      |     | 0.00000             |
     |
| TimeStamp   | datetime         |      |     | 0000-00-00 00:00:00 |
     |
| JobRef      | text             |      |     |                     |
     |
+-------------+------------------+------+-----+---------------------+-----------
-----+
7 rows in set (0.00 sec)

因此,所有查询都应该返回 costingjobtimes 中的所有行,但输入员工姓名而不是 EmployeeID 和部门描述而不是 DeptID。任何帮助都会很棒..

提前致谢,

【问题讨论】:

    标签: mysql inner-join


    【解决方案1】:

    我不知道 Inner Join 是否真的是您正在寻找的 teishu。也许你应该尝试正常的加入。

    例如:

    select e.Name, d.DeptDesc, j.JobNumber, j.TimeElapsed, j.TimeStamp, j.JobRef
      from costingjobtimes as j
      join costingdepartment as d on d.DeptID = j.DeptId
      join costingemployee as e on e.EmployeeID = j.EmployeeID
    

    这应该会为您提供一个结果集,其中包含 costingjobtimes 表中的信息以及员工姓名和部门描述。

    希望有帮助。

    【讨论】:

    • 谢谢。。这是更好的方法,但是我发现问题是 costingemployee 表中缺少一个索引,因此该索引的所有记录都丢失了。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多