2017-02-03 9 views
0

テーブルから重複を削除したいgame。同じplayerIdtimeParameterのエントリがあり、最低のものはgameIdのエントリが残ります。エイリアスなしで削除

私はエントリを照会することができます

select a.`gameId` from `game` a 
where not exists 
    (select min(b.`gameId`) as m from `game` b 
    group by b.`playerId`, b.`timeParameter` 
    having m = a.`gameId`); 

しかし、私はALIS delete文で使用することはできません:構文エラーを取得

delete from `game` a 
where not exists 
    (select min(b.`gameId`) as m from `game` b 
    group by b.`playerId`, b.`timeParameter` 
    having m = a.`gameId`); 

を:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual 
that corresponds to your MariaDB server version for the right syntax to use near 'a 
where not exists 
    (select min(b.`gameId`) as m from `game` b 
    group by b.`' at line 1 

このエラーは何も表示されませんが、削除でエイリアスを使用することはできませんステートメント。

解決方法はありますか?

+0

も、あなたはselect文を使用してこれを削除することはできません同じテーブルで –

+0

これを見てくださいhttp://stackoverflow.com/questions/4562787/how-to-delete-from-select-in-mysql – rpd

+0

@vertexそれとも? –

答えて

1

あなたは、サブクエリを作ることができますし、1つの派生テーブルの正しいだとエイリアスを使用している場合、MySQLのドキュメントごとに、あなたはあなたのDELETE文でそれを参照する必要があります

delete from game where gameId IN 
(
select gameId from(
select a.`gameId` from `game` a 
where not exists 
    (select min(b.`gameId`) as m from `game` b 
    group by b.`playerId`, b.`timeParameter` 
    having m = a.`gameId`) 
)tmp 
) 
+0

ありがとうございますが、うまくいきません: 'ERROR 1093(HY000):テーブル 'game'は、 'DELETE'のターゲットとしても、データの別ソースとしても' krishn patel 'と同じように2回指定されています。 – Vertex

+0

サンプルデータを提供することができますか?試してみてください – sumit

+0

試してみてください........ – sumit

1

を作ります。だから、別名aを削除するか、これは動作するはず一時テーブルを作成するためのアイデアを@krishnパテル使用Documentation

Note

If you declare an alias for a table, you must use the alias when referring to the table:

DELETE t1 FROM test AS t1, test2 WHERE ...

1

から引用

delete a from `game` a ... 

のようなあなたのDELETE声明を出すのいずれか。

最初に表を作成してください。dontDeleteMeには、残すべきgameIdがすべて含まれています。

私はサブクエリについては、この表を使用することができるよりも
create table `dontDeleteMe` as 
     select min(`gameId`) as `theId` from `game` 
     group by `playerId`, `timeParameter`; 

delete from `game` 
where `gameId` not in 
    (select `theId` from `dontDeleteMe`); 

その後、私は、一時テーブルを削除することができます

drop table `dontDeleteMe`; 
+0

あなたは@rahul ansを試すことができますか? –

+0

あなたは派生テーブルを作成し、単一のSQLを使用して行うことができます。これは悪い考えです – sumit

関連する問題