Forum Moderators: coopster
Select * FROM ratings order by id limit 1. What were you saying about sorting them first? If you do do it this way wouldnt it be a strain on server?
Well I don't know the intricates of mySQL, but I'd think you'd be better off having it sorted because it would require less criteria to get a match.
I guess it depends how your inserts work and what you intend to do with the ratings table. You could sort the table every 24 hours and (hopefully) it wouldnt be much of a strain as a query that has to order the id's for every request for the highest ID's in an unsorted order.
If you sorted the ratingID in descending order the query would be
SELECT id FROM ratings LIMIT 1, which should be quite quick!
>Select * FROM ratings order by id DESC limit 1
yep thaat would get the highest ID in the table, as would this
SELECT max(id) FROM ratings
select id from ratings order by id desc limit 3;
would return the 3 highest and the highest. The highest is, of course, the first row returned. So when you fetch your rows, row 1 will be your highest id.
If you are running seperate scripts then:
select max(id) from rating;
is your best bet.
As far as load/performance - these are very low threshold queries, you would hit your max connection threshold before you would have memory or buffer issues.
Also, you might want to run an explain on the first query to see if you need any indexes. I suspect that you may already have an index on your id column:
explain select id from rating order by id desc limit 3;
[mysql.com...]
Scott
$q = "SELECT id
FROM ratings
ORDER BY id DESC LIMIT 3";
Your next line of code would be:
$r = mysql_query($q);
To call the ids you use a loop to iterate through the array because the frist time through the internal pointer of the array is at position 0. To use that item you simply call position 0 or call it by it's associative name 'id' like so:
echo $r_array['id']
OR
echo $r_array[0]
To get each one of the ids add the loop function like so
for ($i=0; $i<3; $i++) {
$r_array = mysql_fetch_array($r);
echo $r_array['id']."<br />";
}
$r_array is the array and the first item in that array is the highest id (as dictacted by the ORDER BY line of the query). By looping and calling mysql_fetch_array you index the array to the next position and then you can use the value or values as you see fit.