sql >> Base de Datos >  >> RDS >> Mysql

Ejecute 2 consultas juntas pero ejecute por separado

Debe unir las dos consultas:

SELECT
    Rest_Details.Resturant_ID,
    Rest_Details.Resturant_name,
    Delivery_Pcode.Pcode,
    Opening_hrs.Open_time,
    Opening_hrs.Closing_time
FROM Rest_Details
JOIN Deliver_Pcode ON Delivery_Pcode.Restaurant_ID=Rest_Details.Restaurant_ID
LEFT JOIN Opening_hrs ON Opening_Hrs.Restaurant_ID=Rest_Details.Restaurang_ID
    AND Day_of_week = WEEKDAY(NOW()) AND CURTIME() BETWEEN Open_time AND Closing_time
WHERE Delivery_Pcode.Pcode LIKE '%$pcode%'

Porque es un LEFT JOIN , Open_time y Closing_time se rellenarán si el restaurante está abierto, de lo contrario serán NULL . Entonces, el PHP que muestra los resultados puede verificar esto:

while ($row_prods = mysqli_fetch_array($sql)) {
    $rest_id = $row_prods['Resturant_ID'];
    $rest_name = $row_prods['Resturant_name'];

    $output .= '<div id="products">' .
            ' <p id="rest_name">' . $rest_name . '</p>';
    if ($row_prods['Open_time']) {
        $output .= '<p> open ' . $row_prods['Open_time'] . ' close ' . $row_prods['Close_time'];
    }
    $output .= "</div>";
    $i++;
}