我有通知表和代理表。在通知表中有一个alert_persons(ENUM:AGENT, SALES, ADMIN, ALL
)。
我的查询在AGENT
或SALES
。
但是,如果选择了ALL选项,那么我想向AGENT
,SALES
和ADMIN
显示该通知。
这是我的查询。
SELECT *
FROM notification n
JOIN agent a ON n.author_id = a.id
AND type_id =1
AND n.type = 'CLIENT'
AND CASE WHEN (
alert_persons = 'ALL'
)
THEN 1
END
此处,输入=通知类别。 id =通知类别的ID 但是,我没有得到正确的结果。
编辑: 小提琴:http://sqlfiddle.com/#!2/318b3/1
答案 0 :(得分:0)
假设你有一个名为 @NOTIFY 的变量,它可以是
'ALL','AGENT','SALES'或'ADMIN'
尝试以下
SELECT *
FROM notification n
JOIN agent a ON n.author_id = a.id
AND type_id =1
AND n.type = 'CLIENT'
AND CASE WHEN (
@Notify = 'ALL' OR alert_persons = @Notify
)
THEN 1
END
Where, type = notification category. id = notification category's id
SQLFiddle:http://sqlfiddle.com/#!2/318b3/4
答案 1 :(得分:0)
我已经解决了它:
SELECT *
FROM notification n
JOIN agent a ON n.author_id = a.id
AND type_id =1
AND n.type = 'CLIENT'
AND CASE WHEN (
alert_persons = 'CLIENT'
)
THEN alert_persons = 'CLIENT'
WHEN (
alert_persons = 'ALL'
)
THEN 1
END