2015-11-04 132 views
0

我試圖讓sql語句鏈接從一個表到另一個,並使用案例(或如果)如果用戶有accesslevel 1那麼聲明應該是select * from campaigns,而如果你不是accesslevel 1你應該得到的選擇語句爲:Select INNER JOIN AND CASE

select * from campaigns WHERE campaign_Creator = ${user_ID} 

SELECT * FROM campaigns 
INNER JOIN users ON campaigns.CAMPAIGN_CREATOR = users.USER_ID 
    CASE WHEN users.USER_ACCESSLEVEL = 1 
    THEN SELECT * FROM campaigns 
    ELSE select * from campaigns WHERE CAMPAIGN_CREATOR = ${user_ID}; 

我會如何去創造這樣一個聲明?我一直在試圖查找它,但是我更加困惑於如何解決這個問題。

+0

樣本數據和期望的結果將是一個很大的幫助。 –

+0

不能這樣做。一個select有一個不變的返回列數,與表數據無關。請加入,使用where(或on)來限制返回的行。 – jarlh

回答

1

也許這就是你想要的?

SELECT * FROM campaigns 
INNER JOIN users ON campaigns.CAMPAIGN_CREATOR = users.USER_ID 
WHERE users.USER_ACCESSLEVEL = 1 
    OR CAMPAIGN_CREATOR = ${user_ID}; 
0

我同意我們的朋友的解決方案,並補充遵循我的建議。

SELECT * 
    FROM campaigns 
    INNER JOIN users ON campaigns.CAMPAIGN_CREATOR = users.USER_ID 
    AND (users.USER_ACCESSLEVEL = 1 OR (users.USER_ACCESSLEVEL != 1 
             AND CAMPAIGN_CREATOR = ${user_ID}))