简体   繁体   English

使用过滤更新另一个表中的记录

[英]updating records from another table with filtering

I'm not an expert in MySQL and I'm facing a problem which I don't know how to solve.我不是 MySQL 专家,我正面临一个我不知道如何解决的问题。

I've two tables.我有两张桌子。

Table `players`
id | name | high_score
---|------|-----------
1  | john | -
2  | mary | -
3  | mike | -

Table `scores`
id | id_player | score
---|-----------|------
1  | 1         | 12
2  | 1         | 5
3  | 3         | 8
4  | 2         | 7
5  | 2         | 25
6  | 3         | 18

This could sound stupid, but I would like to update the high score record of the first table, based on the maximum value on the second table.这听起来很愚蠢,但我想根据第二个表的最大值更新第一个表的高分记录。

I arrived to this solution, which is incomplete:我到达了这个不完整的解决方案:

UPDATE `productos_players` p INNER JOIN `scores` s ON (p.id = s.id_player) SET p.high_score=s.score

This is what I've done so far, but I still need to modify the query for scores to get the maximum MAX() and limit it to 1 result or something similar, but I don't know how to do it, or even if it is possible.这是我到目前为止所做的,但我仍然需要修改scores查询以获得最大 MAX() 并将其限制为 1 个结果或类似的结果,但我不知道该怎么做,甚至如果可能的话。

Thanks for your help!!谢谢你的帮助!!

You can try the following UPDATE to update the high scores:您可以尝试以下UPDATE来更新高分:

UPDATE `players` p 
SET p.high_score = (
    SELECT MAX(score) 
    FROM scores 
    WHERE id_player = p.id
);

demo: http://sqlfiddle.com/#!9/7320c3/2/0演示: http : //sqlfiddle.com/#!9/7320c3/2/0


Another solution would be to use a VIEW :另一种解决方案是使用VIEW

CREATE VIEW v_players AS
    SELECT players.*, MAX(scores.score) AS 'high_score' 
    FROM players LEFT JOIN scores ON players.id = scores.id_player 
    GROUP BY players.id

The advantage of using a VIEW is not to update the table after each change of the scores table.使用VIEW的优点是不会在每次更改scores表后更新表。


How to use the UPDATE automatically using a TRIGGER :如何使用TRIGGER自动使用UPDATE
The UPDATE query can be used on a TRIGGER to UPDATE the table after INSERT , UPDATE or DELETE of the scores table, automatically:UPDATE查询可以在使用TRIGGERUPDATE表后INSERTUPDATEDELETE所述的scores自动表,:

DELIMITER //

-- trigger on UPDATE of table scores.
CREATE TRIGGER upd_players AFTER UPDATE ON scores
FOR EACH ROW
    BEGIN
        UPDATE `players` p SET p.high_score = (
            SELECT MAX(score) 
            FROM scores 
            WHERE id_player = p.id AND id_player = NEW.id_player
        );     
    END;//

-- trigger on INSERT of table scores.
CREATE TRIGGER ins_players AFTER INSERT ON scores
FOR EACH ROW
    BEGIN
        UPDATE `players` p SET p.high_score = (
            SELECT MAX(score) 
            FROM scores 
            WHERE id_player = p.id AND id_player = NEW.id_player
        );     
    END;//   

-- trigger on DELETE of table scores.
CREATE TRIGGER del_players AFTER DELETE ON scores
FOR EACH ROW
    BEGIN
      UPDATE `players` p SET p.high_score = (
          SELECT MAX(score) 
          FROM scores 
          WHERE id_player = p.id AND id_player = OLD.id_player
      );     
    END;//  

DELIMITER ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM