如何在两个日期和时间之间选择记录?次?

时间:2013-01-22 13:07:56

标签: mysql

我有2张桌子。 client (client_id, client_name, client_lname)orders(Order_Id,Client_Id,Total_Amount,Order_Date,Order_Time,Order_Status) Order_Date类型和Order_Time是varchar。 我必须在两个日期和时间之间显示记录。 我写了这个问题:

SELECT 
  o.Order_Id, 
  c.client_name, 
  c.client_lname, 
  o.Total_Amount, 
  o.Order_Date, 
  o.Order_Time 
FROM orders AS o 
JOIN client AS c 
  ON o.Client_id = c.Client_id 
WHERE  (o.Order_Date between '01/01/2013' and '22/01/2013' ) 
AND (o.Order_Time between '17:41:59' and '20:42:04') 
AND o.Order_Status='1' 
ORDER BY o.Order_Id 

代表:

 Order_id   Client_id   Total_Amount             Order_Date        Order_Time Order_Status
    120        32        60                        01/01/2013        12:44:15    1
121        32        60                        01/01/2013        12:47:51    1
122        32        90                        01/01/2013        18:16:41    1
127        32        60                        01/01/2013        18:34:31    1
128        32        90                        01/01/2013        18:35:19    1
129        32       120                        19/01/2013        8:12:04     1
130        32        90                        19/01/2013        15:33:16    0
131        33        90                        19/01/2013        15:40:03    1

我想显示从01/01/2013 12:40:00到19/01/2013 09:00:00的记录。所以它应该显示6条记录。要写什么查询

3 个答案:

答案 0 :(得分:1)

您需要在WHERE子句中组合日期和时间列。

WHERE DATE_ADD(o.Order_Date, INTERVAL o.Order_Time HOUR_SECOND) 
  BETWEEN '2010-01-01 16:30:00' AND '2010-01-02 17:00:00'

答案 1 :(得分:0)

请尝试使用以下日期格式: YYYY-MM-DD HH:MM:SS'01/01/2013'成为'2013-01-01 00:00:00'

For more info see

答案 2 :(得分:0)

首先,我将类型更改为

Order_Date to date和Order_Time to time。

然后我写了这个查询

select o.Order_Id,c.client_name,c.client_lname,o.Total_Amount,o.Order_Date,o.Order_Time from orders as o join client as c on o.Client_id=c.Client_id where  DATE_ADD(o.Order_Date, INTERVAL o.Order_Time HOUR_SECOND) BETWEEN '2012-12-03 11:06:54' AND '2013-01-19 01:07:10'  and o.Order_Status='1' order by o.Order_Id 

有效。就是这样,我期待着结果。 谢谢大家帮我解决问题。