0

Running into some issues when trying to retrieve the last value in a specific column, from a table and assign it into a variable.

Looking for the last int in a column "id" that is a primary key basically.

So I have a variable like "lastValue" in a select statement like :

select last(id) into lastValue from test_table

Not sure on an exact, or best way to accomplish this.

(on mobile, please forgive formatting)

2
  • if id's are ordered ascending you could use "max" Commented Nov 21, 2014 at 1:50
  • You want ORDER BY ... LIMIT 1 Commented Nov 21, 2014 at 2:45

1 Answer 1

1

A typical way to solve this is with order by and limit:

select id
from test_table
order by id desc
limit 1;

Of course, in this case, you could simply use:

select max(id)
from test_table;

But the first method allows you to choose whichever variables you want from the row with the maximum value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Gordon, I'll give the first method a go, I think that's exactly what I'm looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.