I have MySQL Server version 5.1.53. I was looking for an hour to answer this question by myself. Including read the documentation itself at http://dev.mysql.com/doc/refman/5.1/en/select.html
我有MySQL服务器版本5.1.53。我正在寻找一个小时自己回答这个问题。包括在http://dev.mysql.com/doc/refman/5.1/en/select.html上阅读文档本身
Currently, I run this query.
目前,我运行此查询。
SELECT dv2.timestamp
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:25' ORDER BY dv3.timestamp DESC
LIMIT 1
Then I was trying to eliminate the ORDER BY
syntax by determining the calculation of MAX_QUERIES minus 1. By doing that I could write,
然后我试图通过确定MAX_QUERIES减去1的计算来消除ORDER BY语法。通过这样做,我可以写,
SELECT (COUNT(*)-1) total
FROM data_val AS dv2a
WHERE dv2a.timestamp > '2011-06-10 22:26:13'
Finally the query becomes,
最后查询成了,
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
And the error is:
错误是:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( SELECT (COUNT(*)-1) total FROM data_val AS dv2a ' at line 4
#1064 - 您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在第4行使用'(SELECT(COUNT(*) - 1)total FROM data_val AS dv2a'附近使用正确的语法
I also tried to put the subquery after OFFSET
syntax. but still error.
我还尝试在OFFSET语法之后放置子查询。但仍然是错误。
Do you have any idea why my sub-query doesn't work?
你知道为什么我的子查询不起作用吗?
I need technical details with short, simple, and clean explanation.
我需要简短而简洁的技术细节解释。
2 个解决方案
#1
2
From the MySQL manual: http://dev.mysql.com/doc/refman/5.5/en/select.html
从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:
LIMIT子句可用于约束SELECT语句返回的行数。 LIMIT需要一个或两个数字参数,它们都必须是非负整数常量,但有以下例外:
Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
在准备好的语句中,可以使用?指定LIMIT参数?占位符标记。
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
在存储的程序中,LIMIT参数可以使用整数值例程参数或MySQL 5.5.6中的局部变量来指定。
The MySQL query optimizer needs to resolve the limit parameters to a constant before running the query, or it will not know how many rows to return.
MySQL查询优化器需要在运行查询之前将限制参数解析为常量,否则它将不知道要返回多少行。
#2
0
You can't imbed a query result for a limit parameter
您无法嵌入限制参数的查询结果
#1
2
From the MySQL manual: http://dev.mysql.com/doc/refman/5.5/en/select.html
从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:
LIMIT子句可用于约束SELECT语句返回的行数。 LIMIT需要一个或两个数字参数,它们都必须是非负整数常量,但有以下例外:
Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
在准备好的语句中,可以使用?指定LIMIT参数?占位符标记。
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
在存储的程序中,LIMIT参数可以使用整数值例程参数或MySQL 5.5.6中的局部变量来指定。
The MySQL query optimizer needs to resolve the limit parameters to a constant before running the query, or it will not know how many rows to return.
MySQL查询优化器需要在运行查询之前将限制参数解析为常量,否则它将不知道要返回多少行。
#2
0
You can't imbed a query result for a limit parameter
您无法嵌入限制参数的查询结果