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

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

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,

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

I also tried to put the subquery after OFFSET syntax. but still error.

Do you have any idea why my sub-query doesn't work?

I need technical details with short,

simple, and clean explanation.

解决方案

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值