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

标签 mysql

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

任何帮助表示赞赏。

最佳答案

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

关于mysql - 获取第一个匹配条件的结果,不考虑下一个匹配条件的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52895824/

相关文章:

PHP/MySQL 更新查询根本不起作用

mysql - 转换成 View 时sql参数错误

php - 如何将每个嵌套的json放入sql中

mysql - 无法获取批发价格和客户名称 SQL Manager Prestashop

mysql if function - 子查询作为条件

mysql - 在 MySQL 中选择多个字段为 NULL 的行的最优雅方式

PHP Foreach 大数据

php - 如何同步mysql数据库从本地主机更新到在线服务器

mysql - 对 2 个表进行 OR 查询

MySQL - 当行不存在于其他表中或存在且字段大于x时返回