MySQL左连接组中的最低值

时间:2018-10-13 09:52:50

标签: mysql

我有两个桌子。表一包含所有指标。表2包含所有文章。

我正在尝试从度量标准表中获取所有行,并以具有最低优先级的相应文章的品牌,优先级和national_code进行扩展。

SELECT national_code, brand, width, height, inch, `load`, speed, season, rof, MIN(`prio`) as prio FROM `whitelistarticles`
                            WHERE prio < 1000
                            GROUP BY
                            width, height, inch, `load`, speed,season,rof;

                            SELECT metrics.*, articles.national_code, articles.prio, articles.brand FROM whitelistmetrics as metrics
                            LEFT JOIN (SELECT national_code, brand, width, height, inch, `load`, speed, season, rof, MIN(`prio`) as prio FROM `whitelistarticles`
                            WHERE prio < 1000
                            GROUP BY
                            width, height, inch, `load`, speed,season,rof) as articles on
                            metrics.width = articles.width AND
                            metrics.height = articles.height AND
                            metrics.inch = articles.inch AND
                            metrics.load = articles.load AND
                            metrics.speed = articles.speed AND
                            metrics.season = articles.season AND
                            metrics.rof = articles.rof
                            WHERE articles.prio IS NOT NULL
                            ORDER BY width, height, inch, `load`, speed;

问题是此查询为我提供了组中最低的优先级,但没有给我具有优先级的行。最终给我提供了错误的品牌和错误的国家代码,同时它选择了组中最低的优先级。

,例如两篇具有相应指标的文章,其中一篇文章的优先级为20,另一篇文章的优先级为15。我希望该文章的品牌和国家代码的优先级为15。最终,给我优先级为15的国家代码和品牌名称为优先级为20的文章。

如何解决此查询,使其以最低优先级而不是仅优先级加入行?

1 个答案:

答案 0 :(得分:-1)

这将为您提供帮助

SELECT metrics.*, articles.national_code, articles.prio, articles.brand 
  FROM whitelistmetrics as metrics LEFT JOIN 
        (SELECT a1.national_code, a1.brand, a1.width, a1.height, a1.inch, a1.`load`, a1.speed, a1.season, a1.rof, a2.prio
           FROM `whitelistarticles` a1 inner join 
                (SELECT national_code, brand, width, height, inch, `load`, speed, season, rof, MIN(`prio`) as prio 
                   FROM `whitelistarticles`
                  WHERE prio < 1000
                  GROUP BY width, height, inch, `load`, speed, season, rof
                ) a2 on a1.width = a2.width AND
                        a1.height = a2.height AND
                        a1.inch = a2.inch AND
                        a1.load = a2.load AND
                        a1.speed = a2.speed AND
                        a1.season = a2.season AND
                        a1.rof = a2.rof
                        a1.prio = a2.prio
        ) as articles on metrics.width = articles.width AND
                         metrics.height = articles.height AND
                         metrics.inch = articles.inch AND
                         metrics.load = articles.load AND
                         metrics.speed = articles.speed AND
                         metrics.season = articles.season AND
                         metrics.rof = articles.rof
 WHERE articles.prio IS NOT NULL
 ORDER BY width, height, inch, `load`, speed, season, rof;