简体   繁体   English

使用 MySQL 按日期和时间分别选择条目

[英]Selecting entries by date and by time seperately with MySQL

I have this query that selects every appointment with status Pending.我有这个查询选择状态为待处理的每个约会。 Works well.效果很好。 The problem with this query it will also select appointments that are pending in the past.此查询的问题还包括 select 过去未决的约会。 I only want to display those that are either today at a later hour than current_time or simply at a later date.我只想显示那些今天晚于current_time的时间或只是更晚的日期。 Time and date are in a different column.时间和日期在不同的列中。 In the example below only the second and third row should be returned.在下面的示例中,只应返回第二行和第三行。 I'm giving you the full query as it is used and working in my app right now.我正在为您提供完整的查询,因为它现在正在我的应用程序中使用和工作。 How can this be achieved?如何实现?

user_schedule table user_schedule 表

id | customer_id | date            | time     | cleaning_status 
1  | 345         | 2020-06-09      | 08:00:00 | Pending
2  | 768         | 2020-06-09      | 19:00:00 | Pending
3  | 913         | 2020-06-11      | 07:00:00 | Pending

PHP PHP

   if(!empty($_POST)){

    //variables
    $current_time ='16:00:00';
    $current_date ='2020-06-09';
    $my_city ='Miami';
    $sstatus_o = 'Pending';

    //query
    $data = $conn->prepare("select *,us.id as orderid,us.customer_id 
                            as ownerId from user_schedule us 
                            left join users u
                            on us.customer_id=u.id
                            LEFT JOIN user_avatar ua 
                            ON us.customer_id=ua.user_id 
                            and ua.last_update = (select 
                            max(last_update) 
                            from user_avatar ua1 where 
                            ua.user_id=ua1.user_id)
                            left join user_address uad
                            on us.customer_id=uad.user_id
                            where (uad.city LIKE ?) AND 
                            us.cleaning_status=? ORDER BY us.id DESC");
    $data->bind_param('ss',$my_city,$sstatus_o);
    $data->execute();
    $result_data = $data->get_result(); 

    }

You can add another constraint to your query that checks whether the timestamp formed from your date and time values is greater than your $current_date and $current_time values ie您可以向查询添加另一个约束,检查由datetime值形成的时间戳是否大于$current_date$current_time值,即

WHERE uad.city LIKE ?
  AND us.cleaning_status = ?
  AND TIMESTAMP(us.date, us.time) > TIMESTAMP(?, ?)

and then add the $current_date and $current_time variables to the bind_param ie然后将$current_date$current_time变量添加到bind_param

$data->bind_param('ssss', $my_city, $sstatus_o, $current_date, $current_time);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM