How to sort a list by current time and next dated records

Asked

Viewed 58 times

0

I have the following table containing the schedule of the day of the week "Sunday" of a radio:

(id,script,hora_ini,hora_end,link)

I want to get the first 3 programs using as reference the current time.

  • You are using the column with the type TIME?

  • @Atilacardoso: What is the database manager? // How are the hora_ini and hora_fim columns declared? // You want an SQL query or treat directly in PHP code?

  • Yes. I use the time column; The manager is mysql. I prefer a query but if it is not possible the processing with php will be an output as well

  • Welcome to read this post https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-fazer-perguntas/5485#5485

  • Thanks Leo Caracciolo. Next time I will follow the recommendations.

1 answer

2


Evaluate if the following code meets you.

-- código #1
SELECT id, programa, descricao, hora_ini, hora_fim, link
  from tbProgramacao
  where id >= (SELECT id
                 from tbProgramacao
                 where hora_ini <= curtime()
                 order by hora_ini desc
                 limit 1)
  order by hora_ini asc
  limit 3;

To get the programming that is in the air, you can walk in the table in reverse order (from the last to the first), comparing to the current time. Obtained the id of the programming in progress, just get the 3.

It’s been a while since I’ve programmed for Mysql; please test the code carefully.

In the main SELECT WHERE clause (the external one), in place of id you can use hora_ini:

where hora_ini >= (SELECT hora_ini
  • Thank you very much. Saved the days.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.