1

Trying to do something like this:

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

Not workIng for me though.

4
  • Trying to limit the number of results based on the number of events between 6 hours ago and now plus the 100 I'm displaying after now. Commented May 16, 2015 at 2:14
  • looks like you need to be using a union. you want all the events of the last 6 hours, plus the next 100 events, is that correct? Commented May 16, 2015 at 2:26
  • Like how? Not seeing how. Commented May 16, 2015 at 2:28
  • Im not sure, but I think mysql does not support subquery as an argument to LIMIT clause Commented May 16, 2015 at 2:28

2 Answers 2

1

I think you are trying to do something like this:

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;

You cannot use a subquery for the limit parameters.

Sign up to request clarification or add additional context in comments.

2 Comments

What is this e you're using?
The e is an alias assigned to the table reference events. The alias is assigned following the table name in the FROM clause. We use that alias to qualify column references. By adding the alias and a dot before the column name. Here e.g. e.* means all the columns in the events table. (Better practice is to qualify all column references, e.g. WHERE e.start_time . But this is only required when the column reference would be ambiguous without it. In this simple example, it's not apparent how the alias helps us much. But in more complex queries, table aliases are indispensable.
0

You can set a limit variable and use that.

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;

2 Comments

Hello, I am trying to do something like this but i am getting error syntax error @limit this is my query 'SET @limit= ( SELECT COUNT(dispID) FROM dispanser ); select * from counter limit @limit;'
Same Problem for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.