这是db上的books表;
book_ID writer_ID
-------- -----------
1 10
2 10
3 10
4 10
5 10
这是数据库中的费率表,
book_ID rate
------- --------
1 4
2 3
2 5
2 1
2 4
3 5
4 2
4 5
4 2
4 4
5 3
现在,我首先有了 writer_ID,我必须从费率表中找到所有 book_ID(连接到那个 writer_ID)和每个 book_ID 的平均费率。最后,我必须找到最大的平均评分及其 book_ID
这是我的代码
$query="SELECT * FROM books WHERE seller_id ='$id'";
$result = mysql_query($query);
while ($info = mysql_fetch_array($result)) {
//getaveragerate is the function that returns average of the rates from rates table
$arr = array(ID => $info['book_ID'], average => getaveragerate($info['book_ID']));
}
$greatest_average_and_books_id_number = max($arr); // dont know how to get highest average and its ID together from array
这是我的问题,抱歉,英语不是我的母语,我正在尽力解释我的问题。有时我不能,我只是卡住了。
感谢您的理解。
最佳答案
或者让数据库为你做:
SELECT max(fieldname) FROM rates WHERE id='34'
关于php - 如何从数组中找到最大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1656054/