Mysql - LIMIT by Percentage?

后端 未结 2 1356
日久生厌
日久生厌 2020-12-10 15:16

Say I define an alias \'count\' in my Select Query and I want to limit the amount returned to count / 5 (or 20% of the table).

How can I do this? Mysql doesn\'t s

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 15:35

    the LIMIT clause can takes 2 arguments and must be integers constants.

    you can try something like this

    SET @skip=1; SET @numrows=(select count(*) div 5 from tbl );
    PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?, ?';
    EXECUTE STMT USING @skip, @numrows;
    

提交回复
热议问题