mySQL子查询限制

时间:2011-08-19 16:11:33

标签: mysql

这可能很简单......如何通过此查询实现我想要的目标:

delete from posts where id not in
(SELECT id FROM posts order by timestamp desc limit 0, 15)

所以,简而言之,我想删除最新的15个帖子。

当我尝试该查询时,我得到了

  

MySQL尚不支持'LIMIT& IN / ALL / ANY / SOME子查询

修改

mySQL Server version: 5.5.8
mySQL Client version: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $
  

错误:#1235 - 此版本的MySQL尚不支持'LIMIT&   IN / ALL / ANY / SOME子查询'

3 个答案:

答案 0 :(得分:143)

试试这个:

DELETE 
FROM posts 
WHERE id not in (
      SELECT * FROM (
            SELECT id 
            FROM posts 
            ORDER BY timestamp desc limit 0, 15
      ) 
      as t);

答案 1 :(得分:9)

你可以试试这个:

DELETE 
    p1.* 
FROM 
    posts p1 INNER JOIN 
    (SELECT 
            id 
    FROM 
            posts 
            ORDER BY timestamp DESC 
            LIMIT 0, 15
    ) AS p2 
ON p1.id = p2.id;

答案 2 :(得分:1)

因为最新的15将始终来自前15个,如果你按降序排序它们。你可以删除任何没有进入前15的id。就像这样我只是尝试了它并且它工作正常。希望它可以帮助某人

Delete from `table` where id not in (SELECT * FROM (Select id from `table` order by id desc limit 15) as derivedTable);