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

nwwlzxa7  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(419)

我需要从第一个匹配的条件中得到结果,如果它在该级别中找到结果,则从该级别返回或转到下一级别以找到结果。
谢谢你的帮助。

5sxhfpxr

5sxhfpxr1#

这是一个优先级排序查询(有关系)。一种方法使用 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;

相关问题