选择INNER JOIN和CASE

时间:2015-11-04 12:48:23

标签: sql

我正在尝试将sql语句从一个表链接到另一个表并使用case(或if)如果用户具有accesslevel 1则语句应为select * from campaigns,而如果ur不是accesslevel 1 u应该将select语句作为:

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};

我如何创建这样的声明?我一直在努力查找它,但我对如何解决这个问题感到更加困惑。

2 个答案:

答案 0 :(得分:1)

也许这就是你想要的?

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

答案 1 :(得分: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}))