DotDragnet
September 03, 2010, 09:05:32 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Voting for 'The Best Of' the DDN photo comp winners is open, go to http://bit.ly/cSD7qq and have your say for a year in pictures
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Easy way to get min record?  (Read 512 times)
Charisma Bypass
Sr. Member
****
Posts: 382



View Profile Awards
« on: February 05, 2010, 01:27:30 PM »

Is there an -easy- way to get the minimum value from a databse query (without running a previous sql to grab the lowest number);

SELECT * from table WHERE id ={lowest existing id}


Thanks in advance
Logged
Tony
Administrator
Hero Member
*****
Posts: 969



View Profile WWW Awards
« Reply #1 on: February 05, 2010, 01:33:41 PM »

can't you select * from table order by id asc then just pull out the first record?
Logged

neal
Newbie
*
Posts: 48



View Profile Awards
« Reply #2 on: February 05, 2010, 03:30:08 PM »

SELECT * FROM [tableName] ORDER BY [id] ASC LIMIT 1
Logged
keri
Sr. Member
****
Posts: 403



View Profile WWW Awards
« Reply #3 on: February 05, 2010, 04:12:54 PM »

SELECT * FROM [tableName] ORDER BY [id] ASC LIMIT 1

and if it is MS SQL it would end up as SELECT TOP 1 * FROM TABLENAME ORDER BY ID
Logged

Keri
JasonD
Global Moderator
Sr. Member
*****
Posts: 420



View Profile Awards
« Reply #4 on: February 05, 2010, 04:31:59 PM »

Two queries would be faster, SELECT MIN(id).... and .... WHERE id=?, in theory at least. You'd probably need a lot of data to notice.
Logged
Jeep Stone
Hero Member
*****
Posts: 866



View Profile WWW Awards
« Reply #5 on: February 05, 2010, 04:54:04 PM »

If you only wanted the id, don't use *. In fact, don't use * at all, only return the fields you want.
SELECT * FROM [tableName] ORDER BY [id] ASC LIMIT 1

and if it is MS SQL it would end up as SELECT TOP 1 * FROM TABLENAME ORDER BY ID
Logged

neal
Newbie
*
Posts: 48



View Profile Awards
« Reply #6 on: February 05, 2010, 05:37:19 PM »

If you only wanted the id, don't use *. In fact, don't use * at all, only return the fields you want.
SELECT * FROM [tableName] ORDER BY [id] ASC LIMIT 1

and if it is MS SQL it would end up as SELECT TOP 1 * FROM TABLENAME ORDER BY ID

yeah, i would have said that too but was in a rush to get to a presentation this morning. only return the fields you need. it means more typing on your part but it also means a faster query, less data being passed between mysql & php (or mssql & asp), less to go wrong if you change the table structure & more secure scripts.
Logged
Charisma Bypass
Sr. Member
****
Posts: 382



View Profile Awards
« Reply #7 on: February 07, 2010, 06:31:01 PM »

Thank You guys, I do appreciate it!  Cheers
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!