使用左连接在from子句中添加一个表

时间:2014-08-12 09:19:09

标签: sql ms-access left-join

嗨,请与我一起检查这个SQL查询

SELECT 
    CAISSE.*
    ,IIf(USERS.USR_NOM_L IS NULL,CAISSE.USR_L, USERS.USR_NOM_L + ' ' + USERS.USR_PREN_L) AS CLIENT_L
    ,IIf(SOCIETE.SOC_ID = 0 ,CAISSE.SOC_L,SOCIETE.REF_L) AS SOC_REF_L 
FROM 
    (   CAISSE 
        LEFT JOIN USERS ON CAISSE.USR_ID = USERS.USR_ID
    ) 
    LEFT JOIN SOCIETE ON CAISSE.SOC_ID = SOCIETE.SOC_ID  
WHERE 
    1=1 ;

我需要添加“and where SOCIETE.SITE='SiteName'” 但是这没有用,因为我没有在SOCIETE子句中选择FROM表。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我感谢每个人都找到了解决问题的方法

SELECT * FROM (SELECT CAISSE.*,IIf(USERS.USR_NOM_L IS NULL,CAISSE.USR_L,

USERS.USR_NOM_L + ' '     + USERS.USR_PREN_L) AS CLIENT_L,

IIf(SOCIETE.SOC_ID = 0 ,CAISSE.SOC_L,SOCIETE.REF_L) AS SOC_REF_L,SOCIETE.SITE


FROM (CAISSE LEFT JOIN USERS ON CAISSE.USR_ID = USERS.USR_ID) 

LEFT JOIN SOCIETE ON CAISSE.SOC_ID = SOCIETE.SOC_ID)


WHERE SOCIETE.SITE='SiteName';