SQL查询查找3个月的数据

时间:2014-02-19 10:16:20

标签: mysql sql date

如何在SQL

中找到当前日期的最后三个月数据
SELECT Category.name 
FROM Category INNER JOIN Transaction 
ON Category.categoryId = Transaction .category_id 
where DATEDIFF(mm,Transaction.date, Now()) <= 3 
GROUP BY Category.name

1 个答案:

答案 0 :(得分:1)

尝试此查询。看看它是否有帮助。

SELECT `Category`.`name` FROM `Category` 
INNER JOIN `Transaction` 
      ON `Category`.`categoryId` = `Transaction`.`category_id` 
WHERE `Transaction`.`date` >= DATE_ADD(NOW(), INTERVAL -3 MONTH)
GROUP BY `Category`.`name`