Got table for main product listing:
| id | name | content |
1 bla .....
2 ble .....
3 blo .....
4 blu .....
And table for scoring products:
| id | productid | score |
1 4 4
2 2 3
3 4 1
4 3 1
3 1 1
3 2 2
Started to write code for product listing, with paginate class > smarty
$stmt = $mysqli->prepare("SELECT id,name,content FROM products LIMIT ?,?"))
$stmt->bind_param('ii', SmartyPaginate::getCurrentIndex('id'), SmartyPaginate::getLimit('id'));
$stmt->bind_result($id, $name, $content);
$stmt->execute();
$count = '0';
while ($stmt->fetch())
{
$array['id'][] = $id;
$array['name'][] = $name;
$array['content'][] = $content;
$count++;
}
rest code...
It works superb for product listings, but now i need to order products accordinally to average got from score table.
I have seen many examples for rankings but still can’t build anything that would work.
Thanks in advance
Camel
select * from product left join ( select avg(score) avgscore, productid from scoring group by productid ) s on s.productid = product.productid order by avgscore limit 0,10
one way to optimize the query store the avg in the product table and update it occasionally.