没有最后N行的SELECT查询

时间:2014-06-09 06:29:03

标签: mysql

如何选择没有最后N(任何整数值)行的mysql表的所有行。

我试过了

SELECT * FROM 
           chat 
         WHERE chat_id NOT IN(
                    SELECT chat_id FROM chat ORDER BY date_time DESC LIMIT 5
                );

但它会出现以下错误

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

我正在获取当前的MySQL版本

mysql> SHOW VARIABLES LIKE 'version';
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| version       | 5.1.33-community |
+---------------+------------------+

1 个答案:

答案 0 :(得分:2)

使用LEFT JOIN过滤掉符合条件的行。

SELECT c1.*
FROM chat AS c1
LEFT JOIN (SELECT chat_id
           FROM chat
           ORDER BY date_time DESC
           LIMIT 5) AS c2 ON c1.chat_id = c2.chat_id
WHERE c2.chat_id IS NULL