用差分列加入三个多表

时间:2017-08-17 05:56:10

标签: sql vb.net

目前用于表LEDGERNUSER是可以的..但我需要加入另一个表LEDGER_HISTORY。对于LEDGER_HISTORY我只想要字段名STATUS ..任何人都可以帮我解决。

SELECT DISTINCT LED.IDX AS IDX,
                CORPCODE,
                UNITTYPECODE,
                UPPERFORMATIONCODE,
                FORMATIONCODE,
                LED.UNITCODE AS UNITCODE,
                LED.STORECODE AS STORECODE,
                LED.SECTIONNO AS SECTIONNO,
                LED.PARTNO AS PARTNO,
                LED.BATCHNO AS BATCHNO,
                UNITITEMCATEGORYCODE,
                LEDGERDATE,
                ENTITLEMENT,
                HOLDING,
                SURPLUS,
                SHORTAGE,
                COSTHOLDING,
                COSTENTITLEMENT,
                INITIALSTOCK,
                EMPLOYQTY,
                FIGHTQTY,
                ITEMAGE1,
                ITEMAGE2,
                ITEMAGE3,
                ITEMAGE4,
                LED.BAH_BEREK,
                LED.JENIS_BEREK,
                JPTD_NO,
                LASTUPDATE,
                LED.ITEMTYPECODE AS ITEMTYPECODE,
                LED.TOOLTYPECODE AS TOOLTYPECODE,
                LED.PAKAIBHGCODE,
                LED.PAKAICODE,
                ITEMNIDX,
                LED.CREATEID AS CREATEID,
                LED.UPDATEID AS UPDATEID,
                LED.CREATEDATE AS CREATEDATE,
                LED.UPDATEDATE AS UPDATEDATE,
                USERID,
                RANKCODE,
                SERVICENO,
                USERNAME,
                DESIGNATIONCODE,
                PASSWORD,
                'OPER' AS GROUPCODE,
                5 AS ACESSID
FROM (LEDGER AS LED) "
LEFT JOIN NUSER ON NUSER.USERID = LED.UPDATEID
WHERE (1=1)
UNION
SELECT STATUS
FROM LEDGER_HISTORY
WHERE STATUS='1'

1 个答案:

答案 0 :(得分:1)

只需将 1作为状态添加到您的select语句,而不是加入或与其他表联合

        Select DISTINCT LED.IDX As IDX, CORPCODE, UNITTYPECODE, UPPERFORMATIONCODE, FORMATIONCODE, LED.UNITCODE As UNITCODE,LED.STORECODE As STORECODE, LED.SECTIONNO As SECTIONNO, LED.PARTNO As PARTNO, LED.BATCHNO As BATCHNO, UNITITEMCATEGORYCODE, LEDGERDATE, ENTITLEMENT, HOLDING, SURPLUS, SHORTAGE, COSTHOLDING, COSTENTITLEMENT, INITIALSTOCK, EMPLOYQTY, FIGHTQTY, ITEMAGE1, ITEMAGE2, ITEMAGE3, ITEMAGE4, LED.BAH_BEREK, LED.JENIS_BEREK, JPTD_NO, LASTUPDATE, LED.ITEMTYPECODE As ITEMTYPECODE, LED.TOOLTYPECODE As TOOLTYPECODE, LED.PAKAIBHGCODE, LED.PAKAICODE, ITEMNIDX, LED.CREATEID As CREATEID, LED.UPDATEID As UPDATEID, LED.CREATEDATE As CREATEDATE, LED.UPDATEDATE As UPDATEDATE,USERID,RANKCODE, SERVICENO, USERNAME, DESIGNATIONCODE, PASSWORD ,'OPER' AS GROUPCODE,5 AS ACESSID
,1 as 'Status' FROM LEDGER AS LED  LEFT JOIN NUSER ON NUSER.USERID = LED.UPDATEID WHERE (1=1)