Fetch the previous value of a php sql table?

Asked

Viewed 91 times

0

I’d like to find a way to know how to effect what I wish.

I have a news platform and I would like you to introduce me next to the previous video that has been inserted. It seems simple because you could just put it to go get the (id)-1 and would take the value. But what if one of the videos were deleted? There would be a value that would be empty and consequently would give a bug. Is there any way to do it?

  • Because they are news, you must in the database date/time of the correct post ? Just take the news X that is being displayed, fetch the date of this news, and check in the bank the first video going back from that date, or also, back an ID, if not found, back one more and so on.

  • You refer to the previous video as the one that was watched by the person?

3 answers

1


If you have a field where you store the news date, you can do:

SELECT <dados> FROM <tabela> WHERE <tabela.campo_data> < <parametro_data_com_data_do_video_atual> ORDER BY <tabela.campo_data> DESC LIMIT 1;

@Edit

If you only use the ID, you can simply replace the data fields with the id’s :

SELECT <dados> FROM <tabela> WHERE <tabela.campo_id> < <parametro_id_com_id_do_video_atual> ORDER BY <tabela.campo_id> DESC LIMIT 1;

1

In fact, if you take id - 1 may happen that the previous video has been deleted. What can be done is to pick up the nearest previous id through the clause LIMIT and ORDER BY:

SELECT video, id FROM tbl_Video WHERE id < $id ORDER BY id DESC LIMIT 1

0

You passing the ID you want on, you’d get the previous record.

SELECT <CAMPO> FROM <TABELA> WHERE ID = (SELECT MAX(ID) FROM <TABELA> WHERE ID 
< <VALOR.ID>)

Browser other questions tagged

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