同一个 table 上的多个连接?
Multiple joins on same table?
我有两个table:
tbl_EmploymentSegmentEM:
╔══════╦═════════════╦════════════╦═══════════════╦═══════════════════════════════╦════════════╦═════════════╦══════════════════════════╦════════════════╗
║ SrNo ║ CIBILTuefID ║ Prospectno ║ ApplicantType ║ ApplicantName ║ SegmentTag ║ AccountType ║ DateReportedandCertified ║ OccupationCode ║
╠══════╬═════════════╬════════════╬═══════════════╬═══════════════════════════════╬════════════╬═════════════╬══════════════════════════╬════════════════╣
║ 1 ║ 1 ║ 718580 ║ APPLICANT ║ RAJKUMAR GIRISHCHANDRA PANDEY ║ E01 ║ 10 ║ 31122014 ║ 02 ║
║ 2 ║ 4 ║ 718638 ║ APPLICANT ║ Anil Kumar Aggarwal ║ E01 ║ 10 ║ 31122014 ║ 01 ║
╚══════╩═════════════╩════════════╩═══════════════╩═══════════════════════════════╩════════════╩═════════════╩══════════════════════════╩════════════════╝
tbl_CIBILFieldDescription:
╔════════╦══════════╦══════════════════════════════╦═══════╦═════════════════════════════╗
║ Header ║ FieldTag ║ FieldName ║ Value ║ ValueDescription ║
╠════════╬══════════╬══════════════════════════════╬═══════╬═════════════════════════════╣
║ PT ║ 03 ║ TelephoneType ║ 03 ║ Office Phone ║
║ EM ║ 03 ║ OccupationCode ║ 01 ║ Salaried ║
║ EM ║ 03 ║ OccupationCode ║ 02 ║ Self Employed Professional. ║
║ EM ║ 03 ║ OccupationCode ║ 03 ║ Self Employed ║
║ EM ║ 03 ║ OccupationCode ║ 04 ║ Others ║
║ EM ║ 05 ║ NetGrossIncomeIndicator ║ G ║ Gross Income ║
║ EM ║ 05 ║ NetGrossIncomeIndicator ║ N ║ Net Income ║
║ EM ║ 06 ║ MonthlyAnnualIncomeIndicator ║ M ║ Net Monthly ║
║ EM ║ 06 ║ MonthlyAnnualIncomeIndicator ║ A ║ Net Annual ║
║ SC ║ 01 ║ ScoreCardName ║ 01 ║ CIBILTUSCR ║
╚════════╩══════════╩══════════════════════════════╩═══════╩═════════════════════════════╝
我正在尝试从 tbl_CIBILFieldDescription
中获取相应值的帐户类型和职业代码描述。
我试过了:
SELECT DISTINCT CIBILTuefID,
Prospectno,
ApplicantType,
ApplicantName,
SegmentTag,
AccountType,
DateReportedandCertified,
OccupationCode,
mst.ValueDescription AS OccupationCodeDesc,
Income,
NetGrossIncomeIndicator,
mst.ValueDescription AS NetGrossIncomeIndicatorDesc,
MonthlyAnnualIncomeIndicator,
DateofEntryforErrorCode,
ErrorCode,
DateofEntryforCIBILRemarksCode,
CIBILRemarksCode,
DateofEntryforErrorDisputeRemarksCode,
ErrorDisputeRemarksCode1,
ErrorDisputeRemarksCode2,
MkrId,
MkdDt
FROM tbl_EmploymentSegmentEM EM
INNER JOIN tbl_CIBILFieldDescription mst
ON 1 = 1
WHERE mst.Header = 'EM'
AND mst.FieldName = 'OccupationCode'
AND mst.Value = EM.OccupationCode
它似乎对 OccupationCode
工作正常,但如果我想从同一个查询中同时获得 OccupationCode
和 AccountType
怎么办?做这个的最好方式是什么?
您可以使用 LEFT JOIN
:
多次加入您的 table
SELECT DISTINCT cibiltuefid,
prospectno,
applicanttype,
applicantname,
segmenttag,
accounttype,
datereportedandcertified,
occupationcode,
mst.valuedescription AS OccupationCodeDesc,
income,
netgrossincomeindicator,
mst1.valuedescription AS NetGrossIncomeIndicatorDesc,
monthlyannualincomeindicator,
dateofentryforerrorcode,
errorcode,
dateofentryforcibilremarkscode,
cibilremarkscode,
dateofentryforerrordisputeremarkscode,
errordisputeremarkscode1,
errordisputeremarkscode2,
mkrid,
mkddt
,mst.ValueDescription AS Occupation
,mst1.ValueDescription AS Account
FROM tbl_employmentsegmentem EM
LEFT JOIN tbl_cibilfielddescription mst
ON mst.fieldname = 'OccupationCode'
AND mst.value = EM.occupationcode
AND mst.header = 'EM'
LEFT JOIN tbl_cibilfielddescription mst1
ON mst1.fieldname = 'AccountType'
AND mst1.value = EM.accounttype
AND mst1.header = 'EM'
我有两个table:
tbl_EmploymentSegmentEM:
╔══════╦═════════════╦════════════╦═══════════════╦═══════════════════════════════╦════════════╦═════════════╦══════════════════════════╦════════════════╗
║ SrNo ║ CIBILTuefID ║ Prospectno ║ ApplicantType ║ ApplicantName ║ SegmentTag ║ AccountType ║ DateReportedandCertified ║ OccupationCode ║
╠══════╬═════════════╬════════════╬═══════════════╬═══════════════════════════════╬════════════╬═════════════╬══════════════════════════╬════════════════╣
║ 1 ║ 1 ║ 718580 ║ APPLICANT ║ RAJKUMAR GIRISHCHANDRA PANDEY ║ E01 ║ 10 ║ 31122014 ║ 02 ║
║ 2 ║ 4 ║ 718638 ║ APPLICANT ║ Anil Kumar Aggarwal ║ E01 ║ 10 ║ 31122014 ║ 01 ║
╚══════╩═════════════╩════════════╩═══════════════╩═══════════════════════════════╩════════════╩═════════════╩══════════════════════════╩════════════════╝
tbl_CIBILFieldDescription:
╔════════╦══════════╦══════════════════════════════╦═══════╦═════════════════════════════╗
║ Header ║ FieldTag ║ FieldName ║ Value ║ ValueDescription ║
╠════════╬══════════╬══════════════════════════════╬═══════╬═════════════════════════════╣
║ PT ║ 03 ║ TelephoneType ║ 03 ║ Office Phone ║
║ EM ║ 03 ║ OccupationCode ║ 01 ║ Salaried ║
║ EM ║ 03 ║ OccupationCode ║ 02 ║ Self Employed Professional. ║
║ EM ║ 03 ║ OccupationCode ║ 03 ║ Self Employed ║
║ EM ║ 03 ║ OccupationCode ║ 04 ║ Others ║
║ EM ║ 05 ║ NetGrossIncomeIndicator ║ G ║ Gross Income ║
║ EM ║ 05 ║ NetGrossIncomeIndicator ║ N ║ Net Income ║
║ EM ║ 06 ║ MonthlyAnnualIncomeIndicator ║ M ║ Net Monthly ║
║ EM ║ 06 ║ MonthlyAnnualIncomeIndicator ║ A ║ Net Annual ║
║ SC ║ 01 ║ ScoreCardName ║ 01 ║ CIBILTUSCR ║
╚════════╩══════════╩══════════════════════════════╩═══════╩═════════════════════════════╝
我正在尝试从 tbl_CIBILFieldDescription
中获取相应值的帐户类型和职业代码描述。
我试过了:
SELECT DISTINCT CIBILTuefID,
Prospectno,
ApplicantType,
ApplicantName,
SegmentTag,
AccountType,
DateReportedandCertified,
OccupationCode,
mst.ValueDescription AS OccupationCodeDesc,
Income,
NetGrossIncomeIndicator,
mst.ValueDescription AS NetGrossIncomeIndicatorDesc,
MonthlyAnnualIncomeIndicator,
DateofEntryforErrorCode,
ErrorCode,
DateofEntryforCIBILRemarksCode,
CIBILRemarksCode,
DateofEntryforErrorDisputeRemarksCode,
ErrorDisputeRemarksCode1,
ErrorDisputeRemarksCode2,
MkrId,
MkdDt
FROM tbl_EmploymentSegmentEM EM
INNER JOIN tbl_CIBILFieldDescription mst
ON 1 = 1
WHERE mst.Header = 'EM'
AND mst.FieldName = 'OccupationCode'
AND mst.Value = EM.OccupationCode
它似乎对 OccupationCode
工作正常,但如果我想从同一个查询中同时获得 OccupationCode
和 AccountType
怎么办?做这个的最好方式是什么?
您可以使用 LEFT JOIN
:
SELECT DISTINCT cibiltuefid,
prospectno,
applicanttype,
applicantname,
segmenttag,
accounttype,
datereportedandcertified,
occupationcode,
mst.valuedescription AS OccupationCodeDesc,
income,
netgrossincomeindicator,
mst1.valuedescription AS NetGrossIncomeIndicatorDesc,
monthlyannualincomeindicator,
dateofentryforerrorcode,
errorcode,
dateofentryforcibilremarkscode,
cibilremarkscode,
dateofentryforerrordisputeremarkscode,
errordisputeremarkscode1,
errordisputeremarkscode2,
mkrid,
mkddt
,mst.ValueDescription AS Occupation
,mst1.ValueDescription AS Account
FROM tbl_employmentsegmentem EM
LEFT JOIN tbl_cibilfielddescription mst
ON mst.fieldname = 'OccupationCode'
AND mst.value = EM.occupationcode
AND mst.header = 'EM'
LEFT JOIN tbl_cibilfielddescription mst1
ON mst1.fieldname = 'AccountType'
AND mst1.value = EM.accounttype
AND mst1.header = 'EM'