DotDragnet
February 08, 2012, 10:52:16 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Mobile users - Our forum is Tapatalk enabled. http://www.tapatalk.com/
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Easy way to get min record?  (Read 966 times)
Charisma Bypass
Hero Member
*****
Posts: 512



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: 1248



@temps
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
Sr. Member
****
Posts: 300



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

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

sig edited due to migrating from .net
keri
Sr. Member
****
Posts: 487



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
Hero Member
*****
Posts: 529



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: 906



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
Sr. Member
****
Posts: 300



nealio82
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

sig edited due to migrating from .net
Charisma Bypass
Hero Member
*****
Posts: 512



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 Valid XHTML 1.0! Valid CSS!