为什么此查询重复结果?

麦克·奥斯卡·回声

关于联接表,我不是很有经验,所以这可能是我联接它们的方式的结果。我不太明白为什么这个查询会重复结果。例如,这应该只返回3个结果,因为对于该特定的作业和修订版,我只有3行,但是返回6,则重复项与前3个完全相同。

          SELECT
               checklist_component_stock.id,
               checklist_component_stock.job_num,
               checklist_revision.user_id,
               checklist_component_stock.revision,
               checklist_category.name as category,
               checklist_revision.revision_num as revision_num,
               checklist_revision.category as rev_category,
               checklist_revision.per_workorder_number as per_wo_num,
               checklist_component_stock.wo_num_and_date,
               checklist_component_stock.posted_date,
               checklist_component_stock.comp_name_and_number,
               checklist_component_stock.finish_sizes,
               checklist_component_stock.material,
               checklist_component_stock.total_num_pieces,
               checklist_component_stock.workorder_num_one,
               checklist_component_stock.notes_one,
               checklist_component_stock.signoff_user_one,
               checklist_component_stock.workorder_num_two,
               checklist_component_stock.notes_two,
               checklist_component_stock.signoff_user_two,
               checklist_component_stock.workorder_num_three,
               checklist_component_stock.notes_three,
               checklist_component_stock.signoff_user_three
               FROM checklist_component_stock
               LEFT JOIN checklist_category ON checklist_component_stock.category
               LEFT JOIN checklist_revision ON checklist_component_stock.revision = checklist_revision.revision_num
               WHERE checklist_component_stock.job_num = 1000 AND revision = 1;

表结构:

checklist_category

一世

checklist_revision

一世

checklist_component_stock

一世

托斯滕·凯特纳

线

LEFT JOIN checklist_category ON checklist_component_stock.category

当然应该是这样的

LEFT JOIN checklist_category ON checklist_component_stock.category = checklist_category.category

其他大多数dbms都将报告语法错误,但MySQL将checklist_component_stock.category视为布尔值。对于MySQL,布尔值是一个数字,对于FALSE为0,对于TRUE为!= 0。因此,每个类别!= 0的checklist_component_stock都将连接到checklist_category中的所有记录。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章