2016-07-11 41 views
0

我需要一些SQL Server查詢的幫助。我有三個表格文件,模塊和文件夾。SQL Server:如何設置條件在哪裏

Document

Id Title type data folderid 

Module

Id label moduleId folderid 

Folder

Id label 

如果type = 1的文件表,我想從module表否則翔獲得folderid ULD採取ID從自身

我想下面的查詢

SELECT m.* 
    FROM modules m 
    LEFT JOIN documents ed ON CAST(m.moduleId AS VARCHAR) = ed.data 
    where m.folderId = 35 OR ed.folderid =35 

但我不能夠如果有人在那裏可以告訴我的病情,我怎麼能做到這一點寫?

預先感謝

+0

嘗試該SELECT米。* FROM 模塊M LEFT JOIN文檔編ON(CAST(m.moduleId AS VARCHAR)= ed.data) 其中(ed.type = 1和m.folderId = 35 ) 或(ed.type <> 1 and ed.folderid = 35) –

回答

1

另一種選擇是的情況下表達。

SELECT m.* 
FROM modules m 
LEFT JOIN documents ed ON CAST(m.moduleId AS VARCHAR) = ed.data 
where case when type = 1 
    then m.folderId 
    else ed.folderid 
    end = 35 
+0

非常感謝:) – user3198315

1
SELECT m.* 
    FROM modules m 
    LEFT JOIN documents ed ON CAST(m.moduleId AS VARCHAR) = ed.data 
    where (type = 1 and m.folderId = 35) 
    OR (type <> 1 and ed.folderid = 35)