LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-17-2008, 04:22 PM   #1
nawthun
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Rep: Reputation: 0
Unhappy Obtain last job_ref number from mysql table


I have 22 fields in my table and would like to sort the job_ref (number) field and obtain the last number used and display it. Can anyone help me with something simple for this?

Tks,
Delirious Daydreamer
 
Old 12-18-2008, 01:32 AM   #2
scheidel21
Senior Member
 
Registered: Feb 2003
Location: CT
Distribution: Debian 6+, CentOS 5+
Posts: 1,323

Rep: Reputation: 100Reputation: 100
Well your SQL statement is fairly easy it will be something like the following

Code:
SELECT MyTable.job_ref FROM MyTable ORDER BY job_ref;
But now getting the one you want may be more difficult, the SQL above should sort in ascending order if the job_ref number an autonumber where the highest one will be the most recent or is there some method as to how it is assigned. for instance I have a table with an arbitrary ID that is an autonumber it auto increments so the highest number will be the newest record but in this table I also have an employeeID field this is an arbitrary number I assign, the highest employeeID may not be the most recent record. If I want the most recent employee added I would need to have code like below to get both fields and use the autonumber field to identify the newest record and the employeeID field to get the most recent employee added.

Code:
SELECT MyTable.ID, MyTable.EmpID FROM MyTable ORDER BY ID;
Let me know if this helps at all or you would like some more advice, your request is a little unclear so I may have misinterpreted what you are asking for.
 
Old 12-19-2008, 03:33 PM   #3
nawthun
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Obtain last job_ref number from mysql table

Quote:
Originally Posted by scheidel21 View Post
Well your SQL statement is fairly easy it will be something like the following

Code:
SELECT MyTable.job_ref FROM MyTable ORDER BY job_ref;
But now getting the one you want may be more difficult, the SQL above should sort in ascending order if the job_ref number an autonumber where the highest one will be the most recent or is there some method as to how it is assigned. for instance I have a table with an arbitrary ID that is an autonumber it auto increments so the highest number will be the newest record but in this table I also have an employeeID field this is an arbitrary number I assign, the highest employeeID may not be the most recent record. If I want the most recent employee added I would need to have code like below to get both fields and use the autonumber field to identify the newest record and the employeeID field to get the most recent employee added.

Code:
SELECT MyTable.ID, MyTable.EmpID FROM MyTable ORDER BY ID;
Let me know if this helps at all or you would like some more advice, your request is a little unclear so I may have misinterpreted what you are asking for.
Hi,

Tks for responding. I'm a newbie with Php & Mysql. I have an "Add New Client" form with 20+ fields where Admin can enter data for new clients. To keep it simple silly (or stupid) KISS I want to:
1. Search the job_ref field and obtain the last number which is in IMP0001, IMP0002, IMP0003, etc. format;
2. Then increment that number by 1 and present it on the blank form (rather than having to keep a separate paper log of the last number used);

I can't get it to do anything worthwhile and was thinking maybe I should drop the IMP prefix from the job_ref field and stick to using numbers only? I guess I could always use a separate procedure to afix the IMP for display purposes.

Tks in advance for any help.
 
Old 12-19-2008, 11:07 PM   #4
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
Muuuuch easier to just let MySQL's auto-increment feature do its job.

You can call the column IMP or whatever you'd like, but as long as it's set to auto_increment, it'll keep track without you having to do so manually. I can't think of a numeric index I haven't used this for.
 
Old 12-21-2008, 07:06 PM   #5
nawthun
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Poetics View Post
Muuuuch easier to just let MySQL's auto-increment feature do its job.

You can call the column IMP or whatever you'd like, but as long as it's set to auto_increment, it'll keep track without you having to do so manually. I can't think of a numeric index I haven't used this for.

Thanks very much for your response. I had finally come to that conclusion. It is rather silly to have a separate field for this sort of function and just by using the id field, that makes it SOOO much simpler.
Thanks again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Obtain the real IRQ number for a PCI device Elliott-t Linux - Kernel 2 09-24-2006 09:05 PM
MySQL: Can't use '(' or ')' in table name Mikessu Linux - Software 0 08-03-2004 03:59 AM
MySQL non-realtime table-by-table mirroring Passive Linux - Software 1 01-20-2004 12:11 PM
How to import MS ACCESS Table including OLE filed into the MySQL Table ? myunicom Linux - General 1 11-28-2003 11:30 AM
deleted mysql table in mysql now cant do anything nakkaya Linux - Software 0 03-18-2003 06:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration