在mysql中显示不同时间范围内的不同数据

agxfikkp  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(332)

我用这个表向用户显示事务记录。但我想展示的数据是从今天下午6点到明天凌晨2点。我尝试使用当前日期来选择数据。然而,我意识到一旦时间过了12点,从下午6点到12点的数据就消失了,只剩下从12点到凌晨2点的数据。所以我创建了一个if-else方法,只显示当前日期下午6点到11点59分的数据,另一个方法显示当前日期下午6点到凌晨2点的数据,但似乎不起作用。我能知道有什么问题吗?

<?php
                            include_once '../tmkdashboard/includes/db.php';
                                $admin_session = $_SESSION['u_admin'];
                                $currenttime = new DateTime('2014-09-08 06:00:00');
                                $hour = $deliverytime->format('H');

                                $get_member = "SELECT * 
                                               FROM transaction_record_tpg
                                               WHERE ";

                                if($hour >= 0 && $hour <= 2 ){

                                        $get_member.= "collect_outlet = '$admin_session'

                                              AND collected_datetime >= CURRENT_DATE - INTERVAL 6 HOUR
                                              AND collected_datetime <= CURRENT_DATE + INTERVAL 2 HOUR";

                                    } elseif($hour >= 18 && $hour <= 23 ) {

                                        $get_member.= "collect_outlet = '$admin_session' 
                                               AND collected_datetime >= CURRENT_DATE - INTERVAL 18 HOUR
                                               AND collected_datetime <= CURRENT_DATE + INTERVAL 17 HOUR";
                            };             
                                $get_member.= ";";

                                $run_customer = mysqli_query($conn,$get_member);

                                $i = 0;

                                while($row_orders = mysqli_fetch_array($run_customer)){

                                $id = $row_orders['trans_id'];

                                $user = $row_orders['user_id'];

                                $debit = $row_orders['m_debit'];

                                $credit = $row_orders['m_credit'];

                                $date = $row_orders['collected_datetime'];

                                $i++;

                            ?>
                        <tr><!-- tr Starts -->

                            <td><?php echo $i; ?></td>
                            <td><?php echo $id; ?></td>
                            <td><?php echo $user; ?></td>
                            <td><?php echo $debit; ?></td>
                            <td><?php echo $credit; ?></td>
                            <td><?php echo $date; ?></td>

                        </tr><!-- tr Ends -->
                            <?php } ?>
jljoyd4f

jljoyd4f1#

要在今天下午6点到明天凌晨2点之间提取事务,请尝试下面的选择查询:

SELECT * 
  FROM transaction_record_tpg
WHERE collect_outlet = '$admin_session'
  AND collected_datetime >= STR_TO_DATE(CONCAT(CURDATE(), ' 18:00:00'),'%Y-%m-%d %H:%i:%s')
  AND collected_datetime <= STR_TO_DATE(CONCAT(DATE_ADD(CURDATE(), INTERVAL 1 DAY), ' 02:00:00'),'%Y-%m-%d %H:%i:%s');

sqlfiddle示例:
http://sqlfiddle.com/#!2014年9月479日

相关问题