Go get the previous value of a sql table in php?

0

I'd like to find a way to know how to do what I want.

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

    
asked by anonymous 15.09.2017 / 18:59

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

In case of just using the ID, you can simply replace the date 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;
    
15.09.2017 / 19:23
1

In fact, if you get id - 1 , the previous video may have been deleted. What can be done is to get the closest previous id through the LIMIT and ORDER BY clause:

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

    
15.09.2017 / 19:31
0

By passing the ID you want to on, you would get the previous record.

SELECT <CAMPO> FROM <TABELA> WHERE ID = (SELECT MAX(ID) FROM <TABELA> WHERE ID 
< <VALOR.ID>)
    
15.09.2017 / 19:57