0

我有以下查询正常工作。

 SELECT * 
    FROM (
         SELECT * 
         FROM table 
         WHERE name IS NULL 
         ORDER BY date DESC 
         LIMIT 20
         ) AS T 
    ORDER BY date ASC;

我想要的是选择该 sql 选择查询的前 5 条记录。因此,根据我阅读的帖子,我只需要添加 TOP 5。请参见下文:

 SELECT TOP 5 * 
        FROM (
             SELECT * 
             FROM table 
             WHERE name IS NULL 
             ORDER BY date DESC 
             LIMIT 20
             ) AS T 
        ORDER BY date ASC;

不幸的是,mysql 告诉我存在语法错误,但我不明白。预先感谢您的回复。干杯。马克

4

2 回答 2

4

只需添加另一个限制..

 SELECT * 
    FROM (
         SELECT * 
         FROM table 
         WHERE name IS NULL 
         ORDER BY date DESC 
         LIMIT 20
         ) AS T 
    ORDER BY date ASC
    LIMIT 5;
于 2012-04-17T11:25:08.347 回答
3

并非所有数据库都支持 TOP 子句

因为它只是为了限制结果的数量,你可以使用 LIMIT

    SELECT  * 
    FROM (
        SELECT * 
        FROM table 
        WHERE name IS NULL 
        ORDER BY date DESC 
        LIMIT 20
    ) AS T 
    ORDER BY date ASC
    LIMIT 5;
于 2012-04-17T11:25:47.863 回答