一起运行2个查询但单独执行

时间:2016-05-27 16:46:17

标签: php mysql

我目前有2个单独的查询,我需要一起运行,但是单独执行,我想知道这样做的最佳做法是什么。

我有一个查询,每次在管理页面中添加一个餐馆时动态打印餐馆,我有一个查询,如果餐厅是开放或关闭的。我认为通过执行2个单独的查询然后将开放时间echo保存到变量并将此变量放入循环中,这会很简单,但是,这似乎不起作用。通过不工作我的意思是错误的id被调用,因为开始hrs查询在动态打印输出查询之前,并且当它在查询工作计划之后,但是然后我的echo / print out变量不起作用。我很震惊,不知道如何前进。

打开小时查询

$query = mysqli_query($dbc, "SELECT * FROM Opening_hrs 
  WHERE Restaurant_ID='$rest_id' AND Day_of_week = DATE_FORMAT(NOW(), '%w')
  AND CURTIME() BETWEEN Open_time AND Closing_time");

$run_qu = $dbc->query($query);

if($run_qu->num_rows>0){
    while($row_qu=$run_qu->fetch_assoc()){
        $message= "open" .$row_qu["Open_time"]."</br>";
    }
} else {
    $message=$message. "close".$row_qu["Closing_time"]."</br>";
}

动态查询

$sql = mysqli_query($dbc, "SELECT Rest_Details.Resturant_ID,  Rest_Details.Resturant_name,,Delivery_Pcode.Pcode 
 FROM Rest_Details INNER JOIN Delivery_Pcode
 ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
 WHERE Delivery_Pcode.Pcode LIKE '%" . $pcode . "%'") or die("could not search!");
echo var_dump($sql);

$count = mysqli_num_rows($sql);
if ($count === 0) {
    $output = '<b>we dont deliver to ' . $pcode . '</b></br>';
} else {
    $i = 1;
}
while ($row_prods = mysqli_fetch_array($sql)) {
    $rest_id = $row_prods['Resturant_ID'];
    $rest_name = $row_prods['Resturant_name'];

    $output = $output . '<div id="products">' .
            ' <p id="rest_name">' . $rest_name . '</p>' .
            '<p> '.$message.' </p>' .;
    $i++;
}
}

2 个答案:

答案 0 :(得分:1)

您应该加入两个查询:

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%'

因为它是LEFT JOIN,所以如果餐厅是开放式的,则会填写Open_timeClosing_time,否则它们将是NULL。因此显示结果的PHP可以检查:

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++;
}

答案 1 :(得分:0)

如果您想要列出所有餐厅的列表,这些餐厅将在一定范围内的plzs及其营业时间内提供,那么您可以使用此查询:

$sql = '
SELECT
    Rest_Details.Resturant_ID,
    Rest_Details.Resturant_name,
    Delivery_Pcode.Pcode,
    EXISTS(
        SELECT *
        FROM Opening_hrs
        WHERE
            Restaurant_ID=' . $rest_id . ' AND
            Day_of_week = DATE_FORMAT(NOW(), "%w") AND
            CURTIME() BETWEEN Open_time AND Closing_time
    ) as isOpen
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode LIKE "%' . $pcode . '%"
';

如果一行或多行与指定的查询匹配,则mysql exist()将返回1,否则将返回0as isOpen会使此值可在名为isOpen的列中进行访问。