Re: Ranking in MySQL
| From: | DL Neil | Date: | Sat, 10 Aug 2002 12:01:45 +0000 |
| Subject: | Re: Ranking in MySQL | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-21091@lists.php.net to get a copy of this message | ||
David,
I have a MySQL table called images, each with a rating from 1-10. The column
is a decimal(10,2). For simplicity's sake, let's say i have the following:
CREATE TABLE images (
image_id int not null auto_increment,
image_path varchar(255) not null,
rating decimal(10,2) not null,
primary key(image_id)
);
What I'm looking to do is be able to pull from the database the ranked
position each image is in. I.e. "this image is ranked 70 out of 121"
=SQL has no ranking method. However you can (1) select data/rating values
out from the table, (2) count how many rows/ratings appear in the table, (3)
sequence the result set by rating to rank them.
=SELECT data from the table and have it sequenced according to the value in
rating:
SELECT ...
ORDER BY rating DESC;
=the DESC is because ranking usually means highest-first!
=When the record/resultset hits PHP, you can use MYSQL_NUM_ROWS() to find
the number of rows returned from the database - and answer the "this image
is ranked ... out of ???" question.
=Now you can run a FOR loop, to extract each row from the result set (from 1
to 'num_rows'). Each cycle through the loop, the for loop counter will
answer the "this image is ranked ??? out of ..." question.
=Ok?
=dn