Mysql - 加入一对多对多

时间:2016-07-12 12:18:22

标签: mysql sql sql-server-2008

我编写了以下代码,以便在Sql Server 2008 R2中加入一对多。此代码通过ID和订单对select语句进行分区,以便我可以获得前三个诊断:

   Left join (select zzAbstractlink, 
                DiagnosisCIHIValue,
                DiagnosisType,
                RowNumber = Row_Number() Over(Partition by zzAbstractlink order by DiagnosisOccurrence)
                from I10_Diagnosis_VR
                where DiagnosisType <> 'M') Dx1 on AB.AbstractID = Dx1.zzAbstractLink and Dx1.RowNumber = 1   
    Left join (select zzAbstractlink, 
                DiagnosisCIHIValue,
                DiagnosisType,
                RowNumber = Row_Number() Over(Partition by zzAbstractlink order by DiagnosisOccurrence)
                from I10_Diagnosis_VR
                where DiagnosisType <> 'M') Dx2 on AB.AbstractID = Dx2.zzAbstractLink and Dx2.RowNumber = 2
    left join (select zzAbstractlink, 
                DiagnosisCIHIValue,
                DiagnosisType,
                RowNumber = Row_Number() Over(Partition by zzAbstractlink order by DiagnosisOccurrence)
                from I10_Diagnosis_VR
                where DiagnosisType <> 'M') Dx3 on AB.AbstractID = Dx3.zzAbstractLink and Dx3.RowNumber = 3

我需要在MySql中编写相同的代码。有谁知道如何实现这个目标?

这是一些示例代码:

create table person as 
(person varchar(10))

create table diagnosis as
( person varchar(10),
occurence varchar(10),
diagnosis varchar(10))


insert into person
values (1),(2)

insert into diagnosis
values (1,2,A),(1,1,B),(1,3,C),(1,4,D),(2,2,A),(2,3,B),(2,1,C)

选择陈述结果:

Person  |DiagnosisOccurence1    |DiagnosisOccurence2
_____________________________________________________
1       |B                      |A
2       |C                      |A

0 个答案:

没有答案