Skip to content
Advertisement

how to get top 5 food in this sql query

cannot get top 5 food for this query. count top 5 food in that restaurant.

<?php
header('Content-Type: application/json');
require('config.php');

$sqlQuery = "SELECT top 5 fd.name as res_name,(SELECT  count(*) from review re where re.favorite_food=fd.id and re.res_id='1' ) AS count FROM  food fd ";

$result = mysqli_query($connection,$sqlQuery);

$data = array();
foreach ($result as $row) {
    $data[] = $row;
}

mysqli_close($connection);

echo json_encode($data);
?>

occurs error

Advertisement

Answer

SELECT fd.name as res_name, count(*)
FROM food fd
JOIN review re ON re.favorite_food = fd.id
WHERE re.res_id = 1
GROUP BY fd.name
ORDER BY count(*) desc
LIMIT 5
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement