从今天到昨天之间选择

时间:2014-04-11 09:26:23

标签: mysql sql date select

我试图在两个日期之间进行选择:

SELECT p.Code, p.Name, sum(h.PA = 1) AS PA, sum(h.PB = 1) AS PB, 
    sum(h.PG = 1) AS PG, sum(h.GoedkeuringDoorNew = 'GF') AS GF, 
    sum(h.GoedkeuringDoorNew = 'SB') AS SB, sum(h.GoedkeuringDoorNew = 'VIA') AS VIA,
    sum(h.Blanco) AS Blanco
FROM psthostess p 
LEFT JOIN `psttodo-uit` h 
    ON h.`Hostess Code` = p.Code 
    AND DATE(h.`afgewerkt tablet datum`) BETWEEN CURDATE() 
    AND (CURDATE() - INTERVAL 1 DAY)
WHERE p.Indienst = 1 GROUP BY p.Code, p.Name

但我没有得到任何结果。

我想要今天和昨天的结果。但是当我这样做时

SELECT *
FROM `psttodo-uit` p
WHERE DATE(p.`afgewerkt tablet datum`) = CURDATE() - INTERVAL 1 DAY

或将CURDATE() - INTERVAL 1 DAY更改为CURDATE()我会得到结果......这是怎么来的?

2 个答案:

答案 0 :(得分:6)

WHERE p.`afgewerkt tablet datum` >= CURDATE() - INTERVAL 1 DAY 
      AND p.`afgewerkt tablet datum` < CURDATE() + INTERVAL 1 DAY 

假设当前日期和时间是:2014年4月11日05:30 PM,您需要的日期时间是在 2014年4月10日00:00:00 4月11日之间,2014 23:59:59 (包括在内)

CURDATE() - INTERVAL 1 DAY => April 10, 2014 00:00:00
CURDATE() + INTERVAL 1 DAY => April 12, 2014 00:00:00

答案 1 :(得分:0)

 Select *  from  emp where joindate between SUBDATE(NOW(),1) and NOW();