homepage Welcome to WebmasterWorld Guest from 54.167.179.48
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Determine four highest numbers from DB
woldie

10+ Year Member



 
Msg#: 3978 posted 1:58 pm on Jun 2, 2004 (gmt 0)

Hi,

Got this interesting problem, I have a table and in there I have a field called itemid which is set to integer, and basically its a list of numbers.

The problem is, how do you go about calculating the 4 highest numbers?

I've tried using SQL command but I don't think I have the version which support sub selects

select * from t where number in (select max(number) from t)

Thanks in advance :o)

 

dwilson

10+ Year Member



 
Msg#: 3978 posted 2:14 pm on Jun 2, 2004 (gmt 0)

Under SQL Server or Access this would be:

Select Top 4 * From t Order By number Desc

I would try something similar in MySQL ... though not all DBMS' implement "Top".

ukgimp

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3978 posted 2:17 pm on Jun 2, 2004 (gmt 0)

well in mysql you could

SELECT number_feild FROM table ORDER DESC LIMIT 0,4

The limit starts the first record and returns the four values. If that does not work it is 4,0 :)

woldie

10+ Year Member



 
Msg#: 3978 posted 2:38 pm on Jun 2, 2004 (gmt 0)

Thanks UKGimp,

That works :o)

I've ammended the query just slightly because it didn't work first time round, I put the 'ORDER by itemid' in the query.

SELECT prop_date,heading FROM newsfeed ORDER by itemid DESC LIMIT 0,4;

Thanks everyone.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved