2011-07-01 74 views
-2

我想顯示除最後兩個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' 
+6

請詳細說明。它以什麼方式不起作用?你想要它做什麼? – Flimzy

+1

所以你想要除了最後兩行以外的所有行? – knittl

+0

是@Knittl這是我想要 – Hitesh

回答

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 
     ) 
+0

謝謝隊友。這也工作,並且需要700ms執行: - 從Failed_Order_EmailIds f1中選擇f1。*,其中f1.email不在((選擇* from(從Failed_Order_EmailIds中選擇電子郵件順序爲id desc limit 2)爲f2)) – Hitesh

+0

@hitech:WHich數據版本更快? –

+0

第二版。加盟一次只需兩倍 – Hitesh