sql查询显示2个日期之间的所有行

时间:2017-04-18 02:41:12

标签: php mysql

我有一个名为booking details的sql表,其中包含以下列:

Booking_Id
Room_Id
Booking_Date
Check_in_Date
Check_out_Date

我想查看2个日期范围内的所有数据行。例如:

$start='2017-04-15'$end= '2017-04-25'

sql查询应该只显示介于这两个日期范围之间的那些行。

SELECT * FROM booking_details
WHERE '2017-04-15' BETWEEN '2017-04-15' AND '2017-04-25'
OR '2017-04-25' BETWEEN '2017-04-15' AND '2017-04-25'

SELECT * FROM booking_details
WHERE '2017-04-15' BETWEEN '2017-04-15' AND '2017-04-25'

SELECT * FROM booking_details
WHERE '2017-04-15' >='2017-04-15' AND '2017-04-15'<='2017-04-25'

我的编码有问题吗?看看编码并让我知道这些问题:

<div class="wrapper row3">
<div id="breadcrumb" class="clear"> 
    <header id="header" class="clear">
        <h1><font size="5"><u>VIEW BOOKING - HOTEL DAFFODILS</u></font></h1>
    </header>
    <br>
    <?php
        if(isSet($_SESSION['name']))
        {
            echo "<font color=#737373 size=5> WELCOME ".$_SESSION['name']." !</font><br>";
        }
        echo "<br>";
        if(isSet($_SESSION['email']))
        {
            $a=$_POST["data_1"];
            $b=$_POST["data_2"];
            date_default_timezone_set('Asia/Calcutta');
            $start=date("Y-m-d", strtotime($a));
            $end=date("Y-m-d", strtotime($b));
            $diff = abs(strtotime($end) - strtotime($start));
            $days = $diff/86400;
            $date=date("Y-m-d");

            $_SESSION['start']=$start;
            $_SESSION['end']=$end;
            $_SESSION['date']=$date;
            $_SESSION['days']=$days;

            include 'db_config.php';
            if($start<= $end)
            {
                $sql="SELECT * FROM booking_details WHERE ".$date." >= '".$start."' AND ".$date." <= '".$end."'";
                $result = $conn->query($sql);
                if ($result->num_rows > 0) 
                {
                    // output data of each row
                    echo "<table>
                            <tr>
                                <th>Booking Id </th>
                                <th>Room Id</th>
                                <th>Booking Date</th>
                                <th>Check-in Date</th>
                                <th>Check-out Date</th>
                                <th>No of Adults</th>
                                <th>No of Children</th>
                                <th>Questions/Comments</th>
                                <th>Total_Amount</th>
                                <th>Advance_Amount</th>
                                <th>Due_Amount</th>
                            </tr>";                         
                    while($row = $result->fetch_assoc()) 
                    {
                        echo "<tr>";
                            echo "<td>" . $row['Booking_Id'] . "</td>";
                            echo "<td>" . $row['Room_Id'] . "</td>";        
                            echo "<td>" . $row['Booking_Date'] . "</td>";
                            echo "<td>" . $row['Check_in_Date'] . "</td>";
                            echo "<td>" . $row['Check_out_Date'] . "</td>";
                            echo "<td>" . $row['No_of_Adults'] . "</td>";
                            echo "<td>" . $row['No_of_Children'] . "</td>";
                            echo "<td>" . $row['Questions_Comments'] . "</td>";
                            echo "<td>"."₹" . $row['Total_Amount'] . "</td>";
                            echo "<td>"."₹" . $row['Advance_Amount'] . "</td>";
                            echo "<td>"."₹" . $row['Due_Amount'] . "</td>";
                        echo "</tr>";
                    }
                    echo "</table>";
                }   
            }
            else
            {
                echo "Please enter the correct STARTING or ENDING dates !";
            }
            $conn->close();
        }
        else
        {
            echo "Please LOGIN first !";
        }       
    ?>      
    <form action="admin_view_booking.php">
        <br>
        <div style="padding-bottom: 18px;"><input type="submit" name="back" value="BACK"/></div>
    </form>

</div>

1 个答案:

答案 0 :(得分:0)

如果您尝试在开始日期和结束日期之间获取预订日期。试试这个。

SELECT * FROM booking_details 在Booking_Date BETWEEN $ start和$ end

之间
<div class="wrapper row3">
<div id="breadcrumb" class="clear"> 
<header id="header" class="clear">
<h1><font size="5"><u>VIEW BOOKING - HOTEL DAFFODILS</u></font></h1>
</header>
<br>
<?php
    if(isSet($_SESSION['name']))
    {
        echo "<font color=#737373 size=5> WELCOME ".$_SESSION['name']." !</font><br>";
    }
    echo "<br>";
    if(isSet($_SESSION['email']))
    {
        $a=$_POST["data_1"];
        $b=$_POST["data_2"];
        date_default_timezone_set('Asia/Calcutta');
        $start=date("Y-m-d", strtotime($a));
        $end=date("Y-m-d", strtotime($b));
        $diff = abs(strtotime($end) - strtotime($start));
        $days = $diff/86400;
        $date=date("Y-m-d");

        $_SESSION['start']=$start;
        $_SESSION['end']=$end;
        $_SESSION['date']=$date;
        $_SESSION['days']=$days;

        include 'db_config.php';
        if($start<= $end)
        {
            $sql="SELECT * FROM booking_details WHERE ".$date." >= Check_in_Date AND ".$date." <= Check_out_Date";
            $result = $conn->query($sql);
            if ($result->num_rows > 0) 
            {
                // output data of each row
                echo "<table>
                        <tr>
                            <th>Booking Id </th>
                            <th>Room Id</th>
                            <th>Booking Date</th>
                            <th>Check-in Date</th>
                            <th>Check-out Date</th>
                            <th>No of Adults</th>
                            <th>No of Children</th>
                            <th>Questions/Comments</th>
                            <th>Total_Amount</th>
                            <th>Advance_Amount</th>
                            <th>Due_Amount</th>
                        </tr>";                         
                while($row = $result->fetch_assoc()) 
                {
                    echo "<tr>";
                        echo "<td>" . $row['Booking_Id'] . "</td>";
                        echo "<td>" . $row['Room_Id'] . "</td>";        
                        echo "<td>" . $row['Booking_Date'] . "</td>";
                        echo "<td>" . $row['Check_in_Date'] . "</td>";
                        echo "<td>" . $row['Check_out_Date'] . "</td>";
                        echo "<td>" . $row['No_of_Adults'] . "</td>";
                        echo "<td>" . $row['No_of_Children'] . "</td>";
                        echo "<td>" . $row['Questions_Comments'] . "</td>";
                        echo "<td>"."₹" . $row['Total_Amount'] . "</td>";
                        echo "<td>"."₹" . $row['Advance_Amount'] . "</td>";
                        echo "<td>"."₹" . $row['Due_Amount'] . "</td>";
                    echo "</tr>";
                }
                echo "</table>";
            }   
        }
        else
        {
            echo "Please enter the correct STARTING or ENDING dates !";
        }
        $conn->close();
    }
    else
    {
        echo "Please LOGIN first !";
    }       
?>      
<form action="admin_view_booking.php">
    <br>
    <div style="padding-bottom: 18px;"><input type="submit" name="back" value="BACK"/></div>
</form>