在三张表的Fetch记录中重复数据(联接查询)SQL

时间:2019-02-27 04:50:32

标签: mysql

需要在SQL中联接四个表。我有四个表,例如catch_centerdetails主键是catchmasterid,catch_geardetails,catch_headerdetails和cath_linedetails。查询被执行,但是它包含重复的数据。请帮助查询以避免重复的数据。

Table 1 

catch_centerdetails 

catchmasterid centername  surveydate    period
1              XXXXX     02-05-20018    morning
2              YYYYY     02-06-20018    evening

Table 2
catch_geardetails

catchmasterid  gearname Total
    1          Gear1        20
    1          Gear2        30
    2          Gear1        20

Table 3 
catch_headerdetails
catchheaderid   catchmasterid   craftname manpower  status
1                      1        DDDD        10        1
2                      1        DDDD        10        1
3                      1        GGGG        5         1
4                      2        HHHH        5         1

Table 4
cath_linedetails
catchlineid catchmasterid    catchheaderid   speciesname  qty
1                   1               1          sp1        10    
2                   1               1          sp2        5 
3                   1               2          sp1        8 
4                   1               2          sp2        7 
5                   1               3          sp1        9 
6                   1               3          sp2        4 
7                   1               4          sp3        4 
8                   1               4          sp4        7 

select ce.catchmasterid,ce.centername,ce.surveydate,ce.period,
ch.craftname,cg.gearname,cl.speciesname,cl.qty 
from catch_centerdetails ce 
INNER JOIN catch_headerdetails ch ON ce.catchmasterid = ch.catchmasterid 
INNER JOIN catch_geardetails cg on cg.catchmasterid = ce.catchmasterid 
Inner Join cath_linedetails cl ON ce.catchmasterid = cl.catchmasterid;

输出-每个数据重复3次。

1 个答案:

答案 0 :(得分:0)

您在createOnClickHandler(event) { return function onClick() { console.log(event); } } bind(event,action) { this.ref.addEventListener(event, createOnClickHandler(event)); } (一对多关系)的相关表中有多于一行。因此,当您加入时,您将获得多个记录。这是正常现象,符合预期。

如果要获取唯一记录,可以像下面的查询一样使用catchmasterid,或者需要与其他列合并以使相关表记录唯一。

distinct
相关问题