java—连接3个或更多表会产生重复的元组

6tr1vspr  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(245)

表结构:

checklist
---------
id, unit_id, tech_id, date
1,  1,       1,       10/16/2018
2,  2,       2,       10/16/2018 

units
-------
unit_id, unit_name
1,       52
2,       21

techs
--------
tech_id, tech_name
1,       John
2,       Smith

清单有外键约束,删除约束和更新级联 unit_id 以及 tech_id 不起作用的查询:

SELECT units.unit_number, checklist.date, techs.tech_name 
FROM checklist
join units on checklist.unit_id 
join techs on checklist.tech_id

结果:

unit_name, tech_name, date
--------------------------
52,        John,      10/16/2018
52,        John,      10/16/2018
52,        John,      10/16/2018
52,        John,      10/16/2018
21,        Smith,      10/16/2018
21,        Smith,      10/16/2018
21,        Smith,      10/16/2018
21,        Smith,      10/16/2018

我不知道为什么我有重复元组,有人能解释为什么会发生这种情况,以及如何更正我的查询?

rjee0c15

rjee0c151#

您需要按如下方式将列连接到列:

SELECT units.unit_number, checklist.date, techs.tech_name 
FROM checklist c
join units u on c.unit_id = u.unit_id
join techs t on c.tech_id = t.tech_id

相关问题