Pull the information last information you will use for comparison

Asked

Viewed 40 times

1

I have a question in SQL, what I want is the code to pull the last information of this column and compare with a label of my site, but I do not know if the Last Insert Id is better or correct to use.

SELECT Km_Atual FROM Rota WHERE Km_Atual as LAST_INSERT_ID(Km_Atual)
  • What is the name of your Primary key field with auto increment?

  • My Primary key’s name is Id

2 answers

1

To pull the last line you can do this:

SELECT Km_Atual FROM Rota ORDER BY Id DESC LIMIT 1;

1


The problem of using the LAST_INSERT_ID(), is that it only returns the value of AUTO_INCREMENT of your table, in your case the id, but if you still wanted to use it, it would look like this:

SELECT LAST_INSERT_ID() FROM Rota;

If you still want the Km_Atual, you can use the MAX() with the id.

SELECT Km_Atual FROM Rota WHERE id=MAX(id); 

And to make the comparison:

if(Km_Atual == label1.Text) //Onde Km_Atual é a string em que você armazenou o que o select retornou e label1 é o nome da sua label.

Browser other questions tagged

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