我有一个mysql查询GROUP BY,HAVING。
查询工作正常现在需要删除相同的记录,当我尝试删除这些记录时,我收到错误。
选择查询
SELECT * FROM user_location_history WHERE (TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE())>30) AND user_id IN (SELECT user_id FROM user_location_history WHERE (TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE()) < 30) GROUP BY user_id HAVING COUNT(1) > 100) ORDER BY id ASC LIMIT 200
删除查询
DELETE FROM user_location_history WHERE (TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE())>30) AND user_id IN (SELECT user_id FROM user_location_history WHERE (TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE()) < 30) GROUP BY user_id HAVING COUNT(1) > 100) ORDER BY id ASC LIMIT 200
错误
错误代码:1093 您无法在FROM子句
中为更新指定目标表'user_location_history'答案 0 :(得分:0)
解决此问题的一种方法是将子查询包装在另一个子查询中:
DELETE FROM user_location_history
WHERE TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE())>30
AND
user_id IN (
SELECT user_id
FROM (SELECT user_id
FROM user_location_history
WHERE TIMESTAMPDIFF(DAY,
FROM_UNIXTIME(location_date/1000),
SYSDATE()) < 30
GROUP BY user_id
HAVING COUNT(1) > 100) AS t)
ORDER BY id ASC LIMIT 200
答案 1 :(得分:0)
将您的选择查询作为嵌套子查询,然后以下将为您工作
DELETE FROM user_location_history
WHERE TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE())>30
AND
user_id IN
(
select * from
(
SELECT user_id
FROM user_location_history
WHERE TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE()) < 30
GROUP BY user_id HAVING COUNT(1) > 100
)t
)
ORDER BY id ASC LIMIT 200