mysql - 按上午和下午时间订购

标签 mysql

我想要实现的是按日期和时间对表格进行排序(首先是上午,然后是下午)。当我使用此代码尝试时(ORDER BY bookingdate DESC,reservationtime ASC),这就是我得到的。 enter image description here

我对日期没有问题,但时间排序不正确。我想要的是先用“am”显示所有时间,然后用“pm”显示时间。

这是我获取日期和时间并将其插入数据库的方法

$reservationDate = date("F j, Y");
$reservationTime = date( g:i a"); 

$insertdata = "INSERT INTO reservations (reservationdate, reservationtime)VALUES('$reservationDate', '$reservationTime')";

这是显示它的代码

<?php
        $query = "SELECT * FROM reservations ORDER BY reservationdate DESC, reservationtime ASC LIMIT " . $this_page_first_result . ',' . $results_per_page;
        $search_result = filterTable($query);
?>

</div>
<!DOCTYPE html>
<html>
<head>
<title>Admin Control</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>

<form action="adminControl.php" method="GET">
<input id="searchBox" type="text" name="valueToSearch" placeholder="type information here">
<input id="searchButton" type="submit" name="search" value="Search">
<div style="overflow-x:auto;">
    <table class="reservations-table">
    <tr>
        <th class="thFirstName">First Name</th>
        <th class="thLastName">Last Name</th>
        <th class="thEmailAddress">Email Address</th>
        <th class="thContactNumber">Contact Number</th>
        <th class="thSpeaker">Speaker</th>
        <th class="thTopic">Topic</th>
        <th class="thLocation">Location</th>
        <th class="thAudience">Audience</th>
        <th class="thCount">Count</th>
        <th class="thTime">Time</th>
        <th class="thDate">Date</th>
        <th class="thAction">Reservation Date</th>
        <th class="thAction">Reservation Time</th>
        <th class="thAction">Status</th>
        <th class="thAction">Action</th>
        <th class="thAction">Action</th>
    </tr>
     <?php while($row = mysqli_fetch_array($search_result)):?>
                <tr>
                    <td><?php echo $row['firstname'];?></td>
                    <td><?php echo $row['lastname'];?></td>
                    <td><?php echo $row['emailaddress'];?></td>
                    <td><?php echo $row['contactnumber'];?></td>
                    <td><?php echo $row['speaker'];?></td>
                    <td><?php echo $row['topic'];?></td>
                    <td><?php echo $row['location'];?></td>
                    <td><?php echo $row['audience'];?></td>
                    <td><?php echo $row['count'];?></td>
                    <td><?php echo $row['time'];?></td>
                    <td><?php echo $row['date'];?></td>
                    <td><?php echo $row['reservationdate']?></td>
                    <td><?php echo $row['reservationtime']?></td>
                    <td><?php echo $row['reservationstatus'];?></td>
                    <td><?php echo "<a style='padding:6px; float:left; background-color:#45a049; color:white; border-radius: 3px;' href='adminControl.php?epr=approve&speakerName=".$row['speaker']."&reservationStatus=".$row['reservationstatus']."&reservationId=".$row['id']."'>approve </a>";?></td>
                    <td><?php echo "<a style='padding:6px; float:left; background-color:red; color:white; border-radius: 3px;' href='adminControl.php?epr=delete&reservationId=".$row['id']."'>delete</a>";?></td>
                </tr>
                <?php endwhile;?>
    </table>
    </form>
</div>
</body>
</html>

最佳答案

据推测,reservationtime 存储为字符串,而不是时间。您应该使用适当的存储方法。

您可以使用str_to_date()轻松转换为时间:

order by str_to_date(reservationtime, '%l:%i %p')

这将考虑上午/下午。

关于mysql - 按上午和下午时间订购,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45661871/

相关文章:

sql - 复杂的sql排序依据

php - 从 INSERT IGNORE 语句获取多个插入 ID

MySQL 查询,从顶 View 电影中随机选择剧集,但仅当状态 = 0 时才基于

mysql - 使用 Hibernate native 查询插入不适用于 java.util.Date

mysql - 订单记录如果在其他表

MySQL 如何将 WHERE 子句应用于除一列之外的所有选定列?

php - 从 utf8_unicode_ci Mysql 表打印文本时出现错误字符

mysql - 显示表和 MYSQL_NUM_ROWS

mysql - 将 T-SQL 转换为 MySQL

mysql - 如果没有记录,我怎么能算作 0?