【问题标题】:Problems joining parent/child node table in MySQL - reduced result set在 MySQL 中加入父/子节点表的问题 - 减少的结果集
【发布时间】:2014-06-02 17:38:45
【问题描述】:

我在填充名为 component_has_component 的表时遇到问题,该表将父节点指向其子节点。

我创建了一个临时表,其中包含来自物料清单 Excel 表的父 ID 和子 ID,并且需要查找与每个组件关联的唯一键。

查询如下:

INSERT
INTO   component_has_component
       (parent_component_key, parent_index, child_component_key, child_index, amount)
SELECT c.component_key, c.component_index, p.component_key, p.component_index, t.amount
FROM   component c, component p, temp_comp_has_comp t
WHERE  t.parent = c.component_id 
  AND  t.child = p.component_id;

但是,此查询返回的行数少于原始 temp_comp_has_comp(16 对 27),我似乎无法找到其根本原因。

这就是 temp_comp_has_comp 的样子:

id  parent   child      amt
1   A014_06  A033_06    1
2   A231_01  A033_06    1
3   A230_01  A045_04    2
4   A257_01  A0568      1
5   A231_01  A167_01    1

我加入的方式是否有问题,例如重复的 id 被省略了吗?

【问题讨论】:

  • 您确定所有父 ID 和子 ID 都实际存在于组件表中吗?也许其中一些丢失了,这可以解释为什么你得到更少的行。
  • 是的,请显示来自component 的一些行。
  • 请同时发布component 表中的样本。您在这里使用隐式内部连接(逗号分隔的FROM 子句),但您可能需要使用LEFT JOIN

标签: mysql sql join parent-child resultset


【解决方案1】:

谢谢大家,你是对的。我完全忽略了这种可能性,我们的导入宏有问题,现在已修复。

对于组件表,缺少 5 个条目,现在从上述查询返回 27 个结果。

最好, 马吕斯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 2021-02-25
    相关资源
    最近更新 更多