mysql组中最小值的左连接

m3eecexj  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(283)

我有两张table。表一包括所有的指标。表二包括所有条款。
我试图从metric表中得到扩展了品牌、优先级和具有最低优先级的相应商品的国家代码的所有行。

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。
如何修复此查询,使其连接优先级最低的行,而不是仅连接优先级最低的行?

lo8azlld

lo8azlld1#

这对你有帮助

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;

相关问题