简体   繁体   English

两个表的内部连接,带有子句

[英]Inner join on two tables, with clause

with one as
    (Select sno = ROW_NUMBER()OVER (order by complaint_id), Complaint_Id, Complaint.ComplaintType_id, Complaint.complaintProfileId, Complaint.Description, 
                                Complaint.Email, Complaint.PriorityLevel_id, Complaint.Date_Complained, Complaint.Status, Complaint.AdminComments, Complaint.Phone, Complaint.Evidence
                    from Complaints Complaint )

The result of this query is (not the entire result)此查询的结果是(不是整个结果)

sno    complaintProfileId
1              14
2              15
3              15
4              14
5              14
6              13

The second subquery:第二个子查询:

two as
                    (SELECT Complaint.complaintProfileId,
          CASE 
               WHEN MMB_Name IS NOT NULL THEN MMB_Name  
               WHEN UPPMembership.profile_id  IS NOT NULL THEN 'UPP'
               ELSE 'Not found'
          END as Name
      FROM Complaints Complaint
         LEFT JOIN  MMBMembership 
         ON MMBMembership.profile_id = Complaint.complaintProfileId
         left JOIN MMB_BusinessProfiles mmbProfiles
         ON mmbProfiles.MMB_id = MMBMembership.MMB_id
         LEFT JOIN UPPMembership
         ON UPPMembership.profile_id = Complaint.complaintProfileId)


complaintProfileId  Name
14                  UPP
15                Marlon
15                Marlon
14                  UPP
14                  UPP
13                 Rodolfo

So this is where I am having trouble with所以这就是我遇到麻烦的地方

select one.*, two.Name 
 from one join two
 on one.complaintProfileId = two.complaintProfileId

This query returns 36 records. id 14 is being returned 9times and 15-6times and so on.. id 14 被退回 9 次和 15-6 次,依此类推..

I am doing a inner join but still not sure我正在进行内部连接,但仍不确定

Thanks Sun谢谢孙

You need to join on a unique key.您需要使用唯一的密钥加入。 Each '14' on the left side is being joined to each of the three '14's on the right side.左侧的每个“14”都连接到右侧的三个“14”中的每一个。 (3x3=9) (3x3=9)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM