Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit Query recordset to 5 results 2

Status
Not open for further replies.

DrumAt5280

Technical User
Sep 8, 2003
194
US
I would like to query my FAQ table but return only the top 5 most viewed questions, how do i limit the recordset to just first 5 results?

Code:
<cfquery name="top5">
 SELECT *
 FROM FAQ
 (limit to top 5 results)
 ORDER BY views DESC
</cfquery>
 
you can use the top five with maxrows as part of your cfquery tag but the real query still returns all the records. cf just trunk's them. if you want to limit the query you'll have to let us know what db your using and... most likely wait for rudy :)

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
I am using MySQL.

I guess i don't care if it returns the whole set because they will be only about 100 records in it.

Thanks for the help.
 
it's always nice when we get an SQL question in the coldfusion forum and the person actually mentions which database it is ...

in mysql, use this syntax --

SELECT * FROM FAQ
ORDER BY views DESC LIMIT 5

in other database, the "top" syntax is totally different

:)


rudy | r937.com | Ask the Expert | Premium SQL Articles
SQL for Database-Driven Web Sites (next course starts July 10 2005)
 
what about using maxrows, does this run the sql first then store only 5 in the query?

<cfquery name="top5" maxrows ="5">
SELECT *
FROM FAQ
ORDER BY views DESC
</cfquery>
 
sorry. Think I need more coffee before posting messages in forums.. Totally missed TruthInSatire'z post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top