Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You haven't told us what fields from the database will be returned by your query. It also looks like you're filtering (WHERE clause) on key column, which should only return one record. Therefore you can strip out everything you have there and only put: </p> <pre><code>$greatest_record = 34; </code></pre> <p>No need for a query at all!</p> <p>With a little more information on what you're doing and what fields you're expecting:</p> <pre><code>$query = "SELECT id, rate FROM rates"; $result = mysql_query($query); $myarray = array(); $greatest_number = 0; while ($row = mysql_fetch_array($result)) { myarray[] = $row; // Append the row returned into myarray if ($row['id'] &gt; $greatest_number) $greatest_number= $row['id']; } // Print out all the id's and rates foreach ($myarray as $row_num =&gt; $row) { print "Row: $row_num - ID: {$row['id']}, Rate: {$row['rate']} &lt;br&gt;"; } print "Highest ID: $greatest_number"; </code></pre> <p>Note that we maintained what was the greatest number at each row returned from the database, so we didn't have to loop through the $myarray again. Minor optimization that could be a huge optimization if you have tens of thousands of rows or more.</p> <p>This solution is on the basis that you actually need to use the ID and RATE fields from the database later on, but want to know what the largest ID is now. Anyone, feel free to edit my answer if you think there's a better way of getting the greatest_number from the $myarray after it's generated.</p> <hr> <h2>Update:</h2> <p>You're going to need several queries to accomplish your task then.</p> <p>The first will give you the average rate per book:</p> <pre><code>SELECT book_id, avg(rate) as average_rate FROM Rates GROUP BY book_id </code></pre> <p>The second will give you the max average rate:</p> <pre><code>SELECT max(averages.average_rate), averages.book_id FROM ( SELECT book_id, avg(rate) as average_rate FROM Rates GROUP BY book_id ) as averages WHERE averages.average_rate = max(averages.average_rate) </code></pre> <p>This will give you a list of books for a given writer:</p> <pre><code>SELECT book_id FROM Books WHERE writer_id = $some_id </code></pre> <p>Don't try to do everything in one query. Mixing all those requirements into one query will not work how you want it to, unless you don't mind many very near duplicate rows.</p> <p>I hope you can use this update to answer the question you have. These SQL queries will give you the information you need, but you'll still need to build your data structures in PHP if you need to use this data some how. I'm sure you can figure out how to do that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload