By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

getting latest filled row

shoonya
100+
P: 161
i am having a table
Expand|Select|Wrap|Line Numbers
  1. table : t1
  2. a1 serial
  3. a2 varchar(20)
now since a1 will be automatically generated so i am using
Expand|Select|Wrap|Line Numbers
  1. insert into t1(a2) values ('foo');
now i want to get the a1 value corresponding to this value

although
Expand|Select|Wrap|Line Numbers
  1. max(a1)
is working but when too many users will be logged in then it will create problems

any suggestion ??

shoonya
Jun 21 '07 #1
Share this Question
Share on Google+
9 Replies


10K+
P: 13,264
i am having a table
Expand|Select|Wrap|Line Numbers
  1. table : t1
  2. a1 serial
  3. a2 varchar(20)
now since a1 will be automatically generated so i am using
Expand|Select|Wrap|Line Numbers
  1. insert into t1(a2) values ('foo');
now i want to get the a1 value corresponding to this value

although
Expand|Select|Wrap|Line Numbers
  1. max(a1)
is working but when too many users will be logged in then it will create problems

any suggestion ??

shoonya
I don't get something or you're missing the fact that you can do
Expand|Select|Wrap|Line Numbers
  1. select a1 from t1 where a2 = 'foo'
Jun 21 '07 #2

shoonya
100+
P: 161
but a2 is not unique man..
a1 is the primary key here

shoonya
Jun 21 '07 #3

10K+
P: 13,264
but a2 is not unique man..
a1 is the primary key here

shoonya
Yep, I was missing something then .
Jun 21 '07 #4

shoonya
100+
P: 161
okk
so any suggestions now ??

shoonya
Jun 21 '07 #5

10K+
P: 13,264
okk
so any suggestions now ??

shoonya
Sorry I'd forgotten about this. Well I don't think it's possible to avoid going through all the values. The options are:

  • If you used a sequence to generate the IDs you can do
    Expand|Select|Wrap|Line Numbers
    1. SELECT currval('sequence_id_seq');
  • Expand|Select|Wrap|Line Numbers
    1. SELECT id FROM tableName ORDER BY id DESC limit 1;
  • Try also
    Expand|Select|Wrap|Line Numbers
    1. SELECT LAST_INSERT_ID();
    but I'm not sure if this works in PostgreSQL
Jun 21 '07 #6

shoonya
100+
P: 161
there is a possibility that another user might insert some data in between
so that the query results are not consistent

i hope you are getting the problem

shoonya
Jun 21 '07 #7

10K+
P: 13,264
there is a possibility that another user might insert some data in between
so that the query results are not consistent

i hope you are getting the problem

shoonya
If you use SELECT currval(sequence_id_seq'); there will be no problem because the specs say it handles multiple connections correctly.
Jun 21 '07 #8

10K+
P: 13,264
If you use SELECT currval(sequence_id_seq'); there will be no problem because the specs say it handles multiple connections correctly.
By the way I got that from here.
Jun 21 '07 #9

shoonya
100+
P: 161
thanks a lot man

shoonya
Jun 21 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.