I observed this skipping rows for this type of expression quite a few times and always wondered whether this is a bug or if it makes sense - at least technically. The purpose of the queries is to generate sequential table content. Mostly DATEs or DATETIMEs.
t
is just any table with at least 30 rows.
This version produces the result as intended:
set @date = "2012-01-01";
select
@date := date_add(@date, interval 1 day) as d
from t
#having d < "2012-01-31"
limit 30
;
output:
'2012-01-02'
'2012-01-03'
'2012-01-04'
...
'2012-01-31'
now including the HAVING condition (so I don't have to use a number to limit how many rows I want to generate, but can simply specify an upper boudary):
set @date = "2012-01-01";
select
@date := date_add(@date, interval 1 day) as d
from t
having d < "2012-01-31"
limit 30
;
output:
'2012-01-03'
'2012-01-05'
'2012-01-07'
...
'2012-01-29'
'2012-01-31'
But please mind - this is a WHY-question - not a HOW-question.
HAVING
without an aggregate function, nor does it make sense to useLIMIT
without anORDER BY
clause. Sets are by definition unordered and you do not know what order will be returned.