如何在SQL中选择2个月和年份?

时间:2018-02-25 01:47:58

标签: mysql sql

如何在11-201702-2018之间选择数据?

我做了这个SQL:

SELECT COUNT(tbl_data.avg)
FROM tbl_data
INNER JOIN tbl_one ON tbl_one.idx = tbl_data.idx
WHERE tbl_data.download_date
BETWEEN (MONTH(tbl_data.download_date) = (MONTH(tbl_one.dt) AND YEAR(tbl_data.download_date) = YEAR(tbl_one.dt))
AND (MONTH(tbl_data.download_date) = MONTH(NOW()) AND 
YEAR(tbl_data.download_date) = YEAR(NOW()))

但显示ZERO结果。怎么做?先谢谢

P.S。 :字段download_date的类型是DATE(YYYY-MM-DD)

1 个答案:

答案 0 :(得分:0)

所以我已经找到了如何实现它。我只是使用这个SQL命令EXTRACT(YEAR_MONTH FROM mytable.date)。我的样本查询将是这样的:

SELECT a.id, b.user_name
FROM a
INNER JOIN b
    ON a.id = b.id
WHERE EXTRACT(YEAR_MONTH FROM a.dl_date) >= EXTRACT(YEAR_MONTH FROM b.up_date)
    AND EXTRACT(YEAR_MONTH FROM a.dl_date) <= EXTRACT(YEAR_MONTH FROM NOW())