gpt4 book ai didi

mysql - 为什么此查询会重复结果?

转载 作者:行者123 更新时间:2023-11-29 08:05:13 24 4
gpt4 key购买 nike

我在连接表方面不是很有经验,所以这可能是我连接表的方式造成的。我不太明白为什么这个查询会重复结果。例如,这应该只返回 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

i

checklist_revision

i

checklist_component_stock

i

最佳答案

线路

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 视为 bool 值。对于 MySQL 来说, bool 值是一个数字,0 表示 FALSE,!= 0 表示 TRUE。因此,类别 != 0 的每个 checklist_component_stock 都连接到 checklist_category 中的所有记录。

关于mysql - 为什么此查询会重复结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22864189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com