我有以下MySQL查询,该查询从我的数据库中的Pokemon表中找到了最近修改和唯一的Spawnpoint_id:
SELECT
t1.spawnpoint_id, t1.last_modified
FROM
pokemon t1
INNER JOIN
(SELECT
MAX(last_modified) last_modified, spawnpoint_id
FROM
pokemon
GROUP BY spawnpoint_id) t2 ON
t1.spawnpoint_id = t2.spawnpoint_id
AND t1.last_modified = t2.last_modified;
我得到了上述我想要的结果。...但是现在,我想删除所有不匹配这些结果的记录。
我试图将查询包装在DELETE .. NOT IN
中:
DELETE FROM pokemon WHERE (spawnpoint_id, last_modified) NOT IN (
SELECT
t1.spawnpoint_id, t1.last_modified
FROM
pokemon t1
INNER JOIN
(SELECT
MAX(last_modified) last_modified, spawnpoint_id
FROM
pokemon
GROUP BY spawnpoint_id) t2 ON
t1.spawnpoint_id = t2.spawnpoint_id
AND t1.last_modified = t2.last_modified) x;
,但是我得到了MySQL语法错误。我一直在搜索几个小时,最后希望这里有人可以帮助我发现自己做错了什么。非常感谢。
编辑:显示Create Table Pokemon;
CREATE TABLE
pokemon
( encounter_id
VARCHAR(50)不为空, spawnpoint_id
VARCHAR(255)不为空, pokemon_id
int(11)不为null, latitude
double not null, longitude
double not null, disappear_time
日期时间不为null, individual_attack
int(11)默认为空, individual_defense
int(11)默认为空, individual_stamina
int(11)默认为空, move_1
int(11)默认为空, move_2
int(11)默认为空, last_modified
dateTime默认为null, time_detail
int(11)不为空, 主键(encounter_id
), 键pokemon_spawnpoint_id
(spawnpoint_id
), 键pokemon_pokemon_id
(pokemon_id
), 键pokemon_disappear_time
(disappear_time
), 键pokemon_last_modified
(last_modified
), 键pokemon_time_detail
(time_detail
), 键pokemon_latitude_longitude
(latitude
,longitude
) )引擎= innodb默认charset = utf8
我认为问题是您使用表pokemon
,您想在该子查询(不允许使用)中删除行。
一个人可以通过首先进行更新说话来解决这个问题,该更新说明要删除要删除的行,然后进行单独的删除语句。请注意,"不得在从部件中使用" - 限制也适用于更新。但是,可以通过使用联接而不是子选择来解决这:
:create table a (
x int,
y int
);
insert into a (x,y) values (1,2),(3,4);
update a a1, (select max(a2.x) as x from a a2) a3 set a1.y = 0 where a1.x = a3.x;
delete from a where y=0