Sql 同一个表上有多个联接?
我有两张桌子: tbl\U就业细分市场项目:Sql 同一个表上有多个联接?,sql,sql-server,join,sql-server-2005,Sql,Sql Server,Join,Sql Server 2005,我有两张桌子: tbl\U就业细分市场项目: ╔══════╦═════════════╦════════════╦═══════════════╦═══════════════════════════════╦════════════╦═════════════╦══════════════════════════╦════════════════╗ ║ SrNo ║ CIBILTuefID ║ Prospectno ║ ApplicantType ║ ApplicantNam
╔══════╦═════════════╦════════════╦═══════════════╦═══════════════════════════════╦════════════╦═════════════╦══════════════════════════╦════════════════╗
║ 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 ║
╚══════╩═════════════╩════════════╩═══════════════╩═══════════════════════════════╩════════════╩═════════════╩══════════════════════════╩════════════════╝
╔════════╦══════════╦══════════════════════════════╦═══════╦═════════════════════════════╗
║ 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\u CIBILFieldDescription:
╔══════╦═════════════╦════════════╦═══════════════╦═══════════════════════════════╦════════════╦═════════════╦══════════════════════════╦════════════════╗
║ 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 ║
╚══════╩═════════════╩════════════╩═══════════════╩═══════════════════════════════╩════════════╩═════════════╩══════════════════════════╩════════════════╝
╔════════╦══════════╦══════════════════════════════╦═══════╦═════════════════════════════╗
║ 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\u 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
对于
职业代码
来说,它似乎还可以,但是如果我希望从同一个查询中同时使用职业代码
和帐户类型
,该怎么办?最好的方法是什么 您可以使用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'
内部连接。。。。ON 1=1
请解释此联接试图实现的功能。这是交叉联接,即类似于具有WHERE子句中指定的联接条件的旧式联接……除了左联接而不是内部联接之外,我正在做完全相同的事情。谢谢,威尔马克回答。