Is is possible to construct a SELECT
statement that contains a WHERE
clause that uses the value from a column in the "next" row?
I have a date and time field date_entered. ie. given a table with a field named "date_entered" with the following values I want a SELECT statement that selects the rows " WHERE date_entered>='current_date' and date_entered<=[NEXT ROW]date_entered
"
I tried Post but not found what I am looking at.
If your records are ordered chronologically, then you may get through with a query like this:
SELECT
t1.*,
min(t2.date_entered) as next_row_date
from mytable t1
join mytable t2
/* ON nothing */
where t1.id < t2.id
and t1.date_entered > NOW()
group by t1.id
having t1.date_entered <= next_row_date
If you really need next row date... You will likely get a better result with a subquery. Something like
SELECT
t1.*,
from mytable t1
where t1.date_entered > NOW()
and t1.date_entered < ( SELECT date_entered from mytable t2 where t2.id > t1.id limit 1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.