Update auf eine Tabelle mit DISTINCT SELECT in mysql

Angenommen ich habe eine Tabelle mit Informationen über eine

game (PRIMARY INT id, TINYINT offline)

... und eine zweite Tabelle mit detail-Infos zum Spiel:

gamedetail (PRIMARY INT id, INT game_id (fk to game table), TINYINT offline) 

details gibts Häufig und aus verschiedenen Programmen. Da ich die offline-Flagge detail. Ich habe keine programmgesteuerte Möglichkeit, das offline-flag des Spiels selbst. (Ich habe das offline-flag des Spiels auf 0 aber, wenn ich einen online-detail). Aber ich möchte, um diese Informationen in der Datenbank über eine update-Abfrage. Die Idee ist, diese WÄHLEN Sie:

SELECT DISTINCT game.id FROM game 
    LEFT JOIN gamedetail AS gdon 
           ON (gdon.game_id = game.id AND gdon.offline = 0)
    LEFT JOIN gamedetail AS gdoff 
           ON (gdoff.game_id = game.id AND gdoff.offline = 1)
WHERE gdoff.id IS NOT NULL AND gdon.id IS NULL;

Das gibt mir schön alle Spiele, wo ich nur offline gamedetails. Also ich würde gerne diese als input für eine UPDATE-Anweisung wie folgt aus:

UPDATE game SET game.offline=1 WHERE game id IN (
    SELECT DISTINCT game.id FROM game 
        LEFT JOIN gamedetail AS gdon 
               ON (gdon.game_id = game.id AND gdon.offline = 0)
        LEFT JOIN gamedetail AS gdoff 
               ON (gdoff.game_id = game.id AND gdoff.offline = 1)
    WHERE gdoff.id IS NOT NULL AND gdon.id IS NULL;

)

Diese leider nicht in mysql, weil ERROR 1093 (HY000): Table 'game' is specified twice, both as a target for 'UPDATE' and as a separate source for data.

Meine Frage ist wie kann ich änderungen an meiner update-Anweisung in etwas, das funktioniert in mysql?

Edit: korrigiert die WHERE-Bedingung

InformationsquelleAutor luksch | 2014-04-04
Schreibe einen Kommentar