mysql - Selecting the date of the previous record when dates are non-consecutive in SQL? -


i using following select statement within cursor in procedure.

set @chng = (   select tcle - cls                  dly                 dt = subdate(tdt, interval 1 day);                 , dly.nms_id = tnms_id); 

tdt date of current record.

the problem have records in table not have consecutive dates. records in dly have dates '11-01-2010' '12-01-2010' , '15-01-2010' due weekend between.

that select statement flawed if run on '15-01-2010' tdt, then:

where dt = '14-01-2010' 

but want select previous records date:

where dt = '12-01-2010' 

so there way select previous records date?

in case, take rows date earlier specified one, sort them date column in descending order , take last one, (the necessary changes present statement highlighted):

set @chng = (   select tcle - cls                  dly                 dt < tdt                 , dly.nms_id = tnms_id                 order dt desc                 limit 1); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -