两个表的内连接,带子句

时间:2011-07-14 00:43:26

标签: sql sql-server-2008

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 )

此查询的结果是(不是整个结果)

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

第二个子查询:

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

所以这就是我遇到麻烦的地方

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

This query returns 36 records. id 14正在返回9次和15-6次等等......

我正在进行内部联接,但仍不确定

由于 太阳

1 个答案:

答案 0 :(得分:1)

您需要加入一个唯一的密钥。左侧的每个'14'都连接到右侧的三个'14'中的每一个。 (3×3 = 9)