MySQL是否可以使用LIMIT语法进行子查询?如果没有,为什么?

我有MySQL Server版本5.1.53.我在找一个小时自己回答这个问题.包括阅读文档本身http://dev.mysql.com/doc/refman/5.1/en/select.html

当前,我运行此查询.

SELECT dv2.timestamp 
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:25' ORDER BY dv3.timestamp DESC 
LIMIT 1

然后,我试图通过确定MAX_QUERIES减1的计算来消除ORDER BY语法.

SELECT  (COUNT(*)-1) total 
FROM data_val AS dv2a
WHERE dv2a.timestamp > '2011-06-10 22:26:13'

最终查询变为

SELECT dv2.timestamp 
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:13' 
LIMIT (
     SELECT  (COUNT(*)-1) total 
     FROM data_val AS dv2a
     WHERE dv2a.timestamp > '2011-06-10 22:26:13'
     ), 1

错误是:

#1064-您的SQL语法有误;检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在第4行的'(SELECT(COUNT(*)-1)total FROM data_val AS dv2a’附近使用

我还尝试将子查询放在OFFSET语法之后.但仍然错误.

您是否知道为什么我的子查询不起作用?

I need technical details with short,
simple, and clean explanation.

解决方法:

从MySQL手册:http://dev.mysql.com/doc/refman/5.5/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 as of MySQL 5.5.6.

MySQL查询优化器需要在运行查询之前将限制参数解析为一个常数,否则它将不知道要返回多少行.

上一篇:c-以十进制打印变量的地址


下一篇:这个语法在Python中意味着什么?