减去日期时间以获取MySQL中的日期和小时

时间:2018-04-19 10:22:20

标签: mysql datetime left-join datetime-format

我有一个连接表,在我想要与主表连接的表上有一个日期时间列名delivery_date_to,我想将delivery_date_to减去当前日期时间,减法结果将以天和小时格式(例如:2天23小时)作为记录。以下是我加入该表格的方式,

SELECT marketplace_orders.entity_id as id, 
       sales_order.delivery_date_to as delivery_date_to,
       (deliver_date_to - current_time = xx days xx hour) as time_left
FROM marketplace_orders 
INNER JOIN sales_order
      ON sales_order.entity_id = marketplace_orders.order_id
      AND sales_order.status IN ("pending","processing")

1 个答案:

答案 0 :(得分:3)

试试这个解决方案:

SELECT 
    marketplace_orders.entity_id as id, 
    sales_order.delivery_date_to as delivery_date_to,
    CONCAT(TIMESTAMPDIFF(Day,deliver_date_to,NOW()), ' days ',
    MOD(TIMESTAMPDIFF(HOUR,deliver_date_to,NOW()),24), ' hour')  as time_left
FROM marketplace_orders 
INNER JOIN sales_order
    ON sales_order.entity_id = marketplace_orders.order_id
       AND sales_order.status IN ("pending","processing")

演示:

  

http://sqlfiddle.com/#!9/9283a8/5