MySQL LIMIT使用SELECT COUNT(*)作为SELECT中的值

时间:2015-05-16 02:12:05

标签: mysql

尝试做这样的事情:

{{1}}

虽然不适合我。

3 个答案:

答案 0 :(得分:1)

我认为你正在尝试做这样的事情:

MYKEY

您不能将子查询用于SELECT e.* FROM events e WHERE e.start_time >= DATE_SUB(NOW(), INTERVAL 6 HOUR) AND e.start_time < NOW() UNION ALL (SELECT e.* FROM events e WHERE e.start_time >= NOW() ORDER BY e.start_time LIMIT 100 ) ORDER BY start_time; 参数。

答案 1 :(得分:1)

不可能像查询一样使用查询作为LIMIT函数的变量。我建议这样的查询:

SELECT *
FROM events
WHERE (
  start_time >= DATE_SUB(NOW(),INTERVAL 6 HOUR) AND
  start_time < NOW()
)
UNION ALL (
  SELECT *
  FROM events
  WHERE start_time >= NOW()
  ORDER BY start_time
  LIMIT 100
)
ORDER BY start_time ASC;

Here是一个有效的方法。

答案 2 :(得分:0)

您可以设置限制变量并使用它。

SET @limit= (
    SELECT COUNT(*) FROM events WHERE
        start_time >= DATE_SUB(NOW(),INTERVAL 6 HOUR)
    AND
        start_time < NOW()
   )+100;

SELECT * FROM events WHERE
    start_time >= DATE_SUB(NOW(),INTERVAL 6 HOUR)
LIMIT @limit
ORDER BY start_time ASC;