473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

getting latest filled row

shoonya
161 100+
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
9 1809
r035198x
13,262 8TB
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
161 100+
but a2 is not unique man..
a1 is the primary key here

shoonya
Jun 21 '07 #3
r035198x
13,262 8TB
but a2 is not unique man..
a1 is the primary key here

shoonya
Yep, I was missing something then .
Jun 21 '07 #4
shoonya
161 100+
okk
so any suggestions now ??

shoonya
Jun 21 '07 #5
r035198x
13,262 8TB
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
161 100+
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
r035198x
13,262 8TB
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
r035198x
13,262 8TB
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
161 100+
thanks a lot man

shoonya
Jun 21 '07 #10

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

Similar topics

4
by: Mxsmanic | last post by:
The require() I'm using in a PHP script has stopped working after I moved from PHP4 and Apache 1.3.x to PHP5 and Apache 2.x. Now I get messages like this: Warning:...
5
by: Jagdip Singh Ajimal | last post by:
I have 6 columns, all with dates within them, i.e. Proposed Start Date 1 Proposed Start Date 2 Proposed Start Date 3 Proposed Finish Date 1 Proposed Finish Date 2 Proposed Finish Date 3 ...
3
by: Matt Herson | last post by:
I am looking for a validation script that will only look at the fields in the cgi form, determine if the fields are filled out, then if one or more are blank will write a message at the top of the...
0
by: shawnk | last post by:
I downloaded and installed the new SQL Server Express Manager tool but it had problems with the latest 'released version of .NET (version I have is 2.0.40607). I looked on MSDN websites but did...
5
by: brulsmurf | last post by:
Ok iam despered, in Main of this code the object 'two' is looking as should be when i print it, but the exact same code in procedure "duTest" makes the object filled up with additional strange...
76
by: kwilder | last post by:
This works, but it doesn't load the latest version of the xml if it was just modified without closing and reopening the browser. Here's the scenario: I have an xml doc called results.xml. It...
5
by: alanspamenglefield | last post by:
Hello group, I have an SQL statement which pulls data from a table as follows: " SELECT tblSites.sites_siteno, " & _ " tblSites.sites_sitename, " & _ " Sum(tblStockResults.stkr_result) AS...
6
by: Brett Romero | last post by:
I have a DLL "file" reference in my project. The DLL has been updated. I recompile my project but it does not see the DLL updates. I remove and add back the DLL. Now I can see the updates. ...
6
by: piyushdabomb | last post by:
Hi All, Currently, I have a SQL query where I need to grab the SECOND latest date by group. COMPONENTID COMPLETIONDATE CBT_HAZCOM 02/26/2007 15:17:00 CBT_HAZCOM 07/31/2007 21:23:00 QS ...
4
by: tkpmep | last post by:
I've just bought an iMac (OS X 10.5.2, will almost immediately jump to 10.5.3), and am looking to install Python on it, and to use it with XCode, Apple's IDE. Some googling suggests that a number...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.