MySQL-查询:文章的最佳排名价格

问题描述

我有这个查询来查找可能的商品价格:

SELECT MAX(ranking),prices.* FROM (
    SELECT 
        case when merchant_id IS NOT NULL then 100 ELSE 0 END 
        + case when invoice_merchant_id IS NOT NULL then 90 ELSE 0 END 
        + case when merchant_group_id IS NOT NULL then 80 ELSE 0 END 
        + case when whitelist_id IS NOT NULL then 700 ELSE 0 END 
        + case when season_id IS NOT NULL then 60 ELSE 0 END 
        AS ranking,price.id,price.article_id
        FROM (
            select  6587 id,1583 article_id,null merchant_id,1 invoice_merchant_id,null whitelist_id,null merchant_group_id,1 season_id union
            select  6618 id,4561 article_id,1 season_id union
            select  6985 id,NULL invoice_merchant_id,1 merchant_group_id,1 season_id union
            select  7015 id,1 merchant_id,null invoice_merchant_id,1 season_id union
            select  9119 id,1 season_id union
            select  9931 id,1 season_id union
            select  9965 id,1 season_id union
            select  9988 id,1 whitelist_id,1 season_id union
            select  10460 id,1 season_id 
            )price
) AS prices
GROUP BY prices.id

结果是

| MAX(ranking)  | ranking   | id    | article_id|
| 150           | 150       | 6587  | 1583      |
| 150           | 150       | 6618  | 4561      |
| 140           | 140       | 6985  | 1583      |
| 160           | 160       | 7015  | 1583      |
| 150           | 150       | 9119  | 4561      |
| 150           | 150       | 9931  | 1583      |
| 60            | 60        | 9965  | 1583      |
| 760           | 760       | 9988  | 1583      |
| 160           | 160       | 10511 | 4561      |

现在,我需要同时获得最高排名的每篇文章的两个价格:

| MAX(ranking)  | ranking   | id    | article_id|
| 760           | 760       | 9988  | 1583      |
| 160           | 160       | 10511 | 4561      |

我尝试过的部分#1

SELECT MAX(ranking),prices.* FROM (
    SELECT 
        case when merchant_id IS NOT NULL then 100 ELSE 0 END 
        + case when invoice_merchant_id IS NOT NULL then 90 ELSE 0 END 
        + case when merchant_group_id IS NOT NULL then 80 ELSE 0 END 
        + case when whitelist_id IS NOT NULL then 700 ELSE 0 END 
        AS ranking,price.article_id
        FROM (
            #subquery query
        )price
) AS prices
GROUP BY prices.article_id

结果:

| MAX(ranking)  | ranking   | id    | article_id|
| 760           | 150       | 6587  | 1583      |
| 160           | 150       | 6618  | 4561      |

我尝试过的部分#2

SELECT * FROM (
    SELECT MAX(ranking) max_ranking,prices.* FROM (
        SELECT 
            case when merchant_id IS NOT NULL then 100 ELSE 0 END 
            + case when invoice_merchant_id IS NOT NULL then 90 ELSE 0 END 
            + case when merchant_group_id IS NOT NULL then 80 ELSE 0 END 
            + case when whitelist_id IS NOT NULL then 700 ELSE 0 END 
            AS ranking,price.article_id
            FROM (
                #subquery query
            )price
    ) AS prices
    GROUP BY prices.id
) outer_prices
GROUP BY article_id
ORDER BY max_ranking desc

结果

| max_ranking   | ranking   | id    | article_id|
| 150           | 150       | 6587  | 1583      |
| 150           | 150       | 6618  | 4561      |

我已经尝试了更多来自stackoverflow的解决方案,但是没有任何效果。有人可以帮忙吗?

解决方法

如果您使用的是MYSQL 8或更高版本,请尝试使用ROW_NUMBER窗口函数。

ROW_NUMBER

select * from(
SELECT row_number() over(partition by id,article_id order by ranking desc) rn,prices.* FROM (
    SELECT 
        case when merchant_id IS NOT NULL then 100 ELSE 0 END 
        + case when invoice_merchant_id IS NOT NULL then 90 ELSE 0 END 
        + case when merchant_group_id IS NOT NULL then 80 ELSE 0 END 
        + case when whitelist_id IS NOT NULL then 700 ELSE 0 END 
        AS ranking,price.id,price.article_id
        FROM (
            #subquery query
        )price
) AS prices
) as main
where rn=1

在MYSQL 8以下尝试此操作。

SELECT  RANKING,ID,ARTICLE_ID FROM (
    SELECT 
        case when merchant_id IS NOT NULL then 100 ELSE 0 END 
        + case when invoice_merchant_id IS NOT NULL then 90 ELSE 0 END 
        + case when merchant_group_id IS NOT NULL then 80 ELSE 0 END 
        + case when whitelist_id IS NOT NULL then 700 ELSE 0 END 
        + case when season_id IS NOT NULL then 60 ELSE 0 END 
        AS ranking,price.article_id
        FROM (
            select  6587 id,1583 article_id,null merchant_id,1 invoice_merchant_id,null whitelist_id,null merchant_group_id,1 season_id union
            select  6618 id,4561 article_id,1 season_id union
            select  6985 id,NULL invoice_merchant_id,1 merchant_group_id,1 season_id union
            select  7015 id,1 merchant_id,null invoice_merchant_id,1 season_id union
            select  9119 id,1 season_id union
            select  9931 id,1 season_id union
            select  9965 id,1 season_id union
            select  9988 id,1 whitelist_id,1 season_id union
            select  10460 id,1 season_id 
            )price
) AS prices
WHERE (ranking,article_id) IN (
(SELECT MAX(ranking),article_id FROM (
    SELECT 
        case when merchant_id IS NOT NULL then 100 ELSE 0 END 
        + case when invoice_merchant_id IS NOT NULL then 90 ELSE 0 END 
        + case when merchant_group_id IS NOT NULL then 80 ELSE 0 END 
        + case when whitelist_id IS NOT NULL then 700 ELSE 0 END 
        + case when season_id IS NOT NULL then 60 ELSE 0 END 
        AS ranking,1 season_id 
            )price
) AS prices
GROUP BY article_id)
)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...