我该怎么写这个查询?

时间:2011-07-01 10:30:23

标签: mysql

我想显示除最后两个id之外的所有行。我试过了:

SELECT f.* FROM Failed_Order_EmailIds f WHERE id NOT IN
 (SELECT id FROM Failed_Order_EmailIds ORDER BY id DESC LIMIT 2) 

这会出现以下错误:

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

1 个答案:

答案 0 :(得分:1)

SELECT f.* 
FROM Failed_Order_EmailIds f 
  LEFT JOIN
    (SELECT id FROM Failed_Order_EmailIds ORDER BY id DESC LIMIT 2)
  AS last2
    ON last2.id = f.id
WHERE last2.id IS NULL

你也可以使用它:

SELECT f.* 
FROM Failed_Order_EmailIds f 
WHERE f.id <
        ( SELECT MIN(id)
          FROM 
            ( SELECT id FROM Failed_Order_EmailIds ORDER BY id DESC LIMIT 2)
            AS last2
        )