从第一个匹配条件获取结果,而不考虑下一个匹配条件的结果

时间:2018-10-19 15:46:02

标签: mysql

我需要从第一个匹配条件中获取结果,如果在该级别找到结果,则从那里返回或转到下一个级别 找到结果。

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

这是一个优先级查询(带有联系)。一种方法使用dense_rank()

select rsc.* 
from (select rsc.*,
             dense_rank() over (order by case when rsc.entityId = :accountId and rsc.entityTypeCode = 'A' then 1
                                              when rsc.entityId = :companyId and rsc.entityTypeCode = 'C' then 2
                                              when rsc.entityId = :issuerId and rsc.entityTypeCode = 'I' then 3
                                              else 4
                                         end) as seqnum
      from CurrentRuleSetContents rsc 
      where (rsc.entityId = :accountId and rsc.entityTypeCode = 'A') or
            (rsc.entityId = :companyId and rsc.entityTypeCode = 'C') or 
            (rsc.entityId = :issuerId and rsc.entityTypeCode = 'I') or 
            (rsc.industryCode = :industryCode)
    ) rsc
where seqnum = 1;