sql(mysql) optimisations 
Author Message
 sql(mysql) optimisations

Hi All

I have a database of products, in which each product has a number of times
it was viewed.  How can i write some sql to select the top 5 "viewed"
products.

I can easily say "select productCode from products order by rank;" but that
will select all products, and even though there's only 250, it doesnt seem
to be the best way to do it.  I want to say "just select 5 records" somehow,
i know it can be done in oracle, how do i do it in mysql?

Cheers,
Dan

--

www.dankeeley.co.uk



Sun, 26 Jun 2005 01:29:23 GMT  
 sql(mysql) optimisations



Quote:
> ...(cut)...
> I can easily say "select productCode from products order by rank;" but
that
> will select all products, and even though there's only 250, it doesnt seem
> to be the best way to do it.  I want to say "just select 5 records"
somehow,
> i know it can be done in oracle, how do i do it in mysql?

SELECT productCode FROM products ORDER BY rank LIMIT 0,5

If you want the biggest first, and I guess you want that, then you should
add not only LIMIT, but also DESC:
SELECT productCode FROM products ORDER BY rank DESC LIMIT 0,5

I hope that's what you were looking for.

Pete



Sun, 26 Jun 2005 01:57:43 GMT  
 sql(mysql) optimisations

Quote:
> SELECT productCode FROM products ORDER BY rank LIMIT 0,5

> If you want the biggest first, and I guess you want that, then you should
> add not only LIMIT, but also DESC:
> SELECT productCode FROM products ORDER BY rank DESC LIMIT 0,5

> I hope that's what you were looking for.

> Pete

It is, Excellent, thanks very much.

Cheers,
Dan



Sun, 26 Jun 2005 02:52:02 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Ada,SQL,MySQL

2. SQL injection - PHP, MySQL, magic_quotes = on

3. PHP, mySQL and connection to a SQL Database

4. ColdFusion, MySQL, SQL Server, ASP.Net Hosting

5. How to read sql file in MYSQL?

6. SQL Server to MySQL thru PHP

7. MySQL + SQL Statements + Quote escaping

8. MySQL + SQL Statements + Quote escaping

9. SML optimisations / parallelism

10. Eiffel Compiler Optimisations (Was Virtual Destructors)

11. Optimisations performed by Verilog simulators?

12. Unwanted TASM optimisations...

 

 
Powered by phpBB® Forum Software