"YoBro" <yo***@wazzup.co.nz> wrote in message
news:AO****************@news.xtra.co.nz...
Hi,
I have a database that stores in a field the number of views for a
product.
I want to write a query that gets the two highest views, so I can then
output the result.
I have this so far, but not sure how to adapt it to get the two highest
views.
mysql_query("SELECT max(views) FROM stock WHERE status='enabled' AND
photo1 != ''");
Micheal was very close:
mysql_query("
SELECT *
FROM stock
WHERE status = 'enabled'
AND photo1 IS NOT NULL
ORDER BY views DESC
LIMIT 2
");
You don't want to use the max function because that would cause you to
receive only one row... containing just the highest view count and no other
data. The querey above will return the entire row for each of the two
records having the two highest values in views. There is no guarantee what
will happen if more than one two rows match the highest value or if two or
more rows match the second highest value. You might want to have additional
fields specified in the Order By clause to control what happens then.
Perhaps a last viewed date so that the most recently viewed of the most
viewed bubbles to the top.
- Virgil