限制子句内的Mysql子查询

标签 mysql

在 mysql 的 limit 子句中可以有一个子查询吗?我试图在一个查询中只从同一个表中选择总行数的 10%。

SELECT * FROM table LIMIT (SELECT (10 * COUNT(*)/100) FROM table);

关于如何实现这一点有什么建议吗?

最佳答案

你问:

Is it possible to have a subquery inside a limit clause in mysql?

答案基本是否定的。

读这个:

http://dev.mysql.com/doc/refman/5.6/en/select.html

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:

Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.

Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables.

关于限制子句内的Mysql子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21541772/

相关文章:

mysql - TIMESTAMPDIFF 给出意想不到的结果

mysql - 简单插入期间innodb全表锁

php - 使用 Doctrine 仅从 ManyToMany 获取 ID 列表

Mysql触发器在循环之前声明

php - 以一种形式显示两个不同的mysql表结果

mysql - Xampp无法连接到MYsql数据库或ProFTPD

mysql - BETWEEN 无法正常工作

php - URL从id重写为标题

mysql - 如何在 MySQL 中向整数列添加正整数约束?

mysql - 当我确实有别名时,每个派生表在 mysql 中都必须有自己的别名错误