MySQL更新,MAX,加入查询

MySQL更新,MAX,加入查询,mysql,sql,Mysql,Sql,我有两张桌子:- manu_table product_id, manufacturer 1, ford 2, ford 3, toyota product_table product_id, score 1, 80 2, 60 3, 40 我想在汇总表中存储每个制造商得分最高的产品id:- summary_table manufacturer, max_score ford, 1 toyota, 3 到目前为止,我已经:- UPDATE summary_table st SET ma

我有两张桌子:-

manu_table
product_id, manufacturer
1, ford
2, ford
3, toyota

product_table
product_id, score
1, 80
2, 60
3, 40
我想在汇总表中存储每个制造商得分最高的产品id:-

summary_table
manufacturer, max_score
ford,   1
toyota, 3
到目前为止,我已经:-

UPDATE summary_table st
SET max_score = (
                 SELECT product_id 
                 FROM (
                       SELECT manufacturer, product_id, max(score) as ms 
                       FROM manu_table 
                       LEFT JOIN product_table USING (product_id) 
                       group by product_id) t)
WHERE st.manufacturer = manu_table.manufacturer;
遇到困难…非常感谢所有帮助。

尝试此查询-

UPDATE summary_table st
  JOIN (
    SELECT mt.manufacturer, MAX(pt.score) max_score FROM manu_table mt
    JOIN product_table pt
      ON mt.product_id = pt.product_id
    GROUP BY mt.manufacturer) t
  ON t.manufacturer = st.manufacturer
SET st.max_score = t.max_score

此查询为最大分数设置产品标识:

UPDATE summary_table st
  JOIN (SELECT t1.* FROM 
  (SELECT mt.*, pt.score FROM manu_table mt JOIN product_table pt ON mt.product_id = pt.product_id) t1
  JOIN (
    SELECT mt.manufacturer, MAX(pt.score) max_score FROM manu_table mt
     JOIN product_table pt
       ON mt.product_id = pt.product_id
     GROUP BY mt.manufacturer
  ) t2
  ON t1.manufacturer = t2.manufacturer AND t1.score = t2.max_score
  ) t
  ON t.manufacturer = st.manufacturer
SET st.max_score = t.product_id

由于我理解这个问题,我认为这是可行的,我使用了
MAX(Product\u ID)
来解决任何重复的问题,其中同一制造商的两个产品可能具有相同的分数,并且两者都是最高分数。您可能希望以不同的方式解析重复项

UPDATE  summary_table
SET     max_score = 
        (   SELECT  MAX(m.Product_ID) [MaxScoreProductID] 
            FROM    manu_table m
                    INNER JOIN product_table p
                        ON m.Product_ID = p.Product_ID
                    INNER JOIN
                    (   SELECT  Manufacturer, MAX(Score) [MaxScore]
                        FROM    manu_table m
                                LEFT JOIN product_table p
                                    ON m.Product_ID = p.Product_ID
                        GROUP BY Manufacturer
                    ) ms
                        ON ms.Manufacturer = m.Manufacturer
                        AND ms.MaxScore = p.Score
            WHERE   m.Manufacturer = summary_table.Manufacturer
            GROUP BY m.Manufacturer
        )

你有什么问题?你有错误吗?您当前查询的输出是什么?这不是OP要求的。这将返回每个制造商的最大得分,而不是每个制造商的最大得分产品ID。是的,您是对的。一开始,我知道max_分数应该是原样的max(分数)。我添加了新的查询。谢谢。这可以工作,但需要29天才能处理product_表中的1100万行。关于更快的方法有什么想法吗?索引是否正确?即使在1100万行上,这也应该运行得相当快。我怀疑是否需要汇总表,汇总视图更好,不需要定期更新,并且应该维护索引。不幸的是,我很少使用MySQL,只使用SQL Server,所以我的建议可能不是最合适的。抱歉。是的,缺少索引(product_table.Score),仍然需要几个小时,当你很快地说这是你的意思吗?这不是我真正的意思,我希望上面的查询最多需要1小时。请记住,外键不是标记,因此可能有必要与键一起创建标记,实际上,我希望对1100万行进行如此简单的更新需要几秒钟,也许几分钟,但肯定不是几小时