MySQL查询选择月错误

时间:2012-12-06 12:40:34

标签: mysql

我已经构建了这个查询,以便列出每个COD_ZONA在当月完成的IMPORTE数量。日期由FECHA_EMIS指示,COD_CLIENT是两个表之间的链接。

SELECT GVA12.FECHA_EMIS, GVA12.IMPORTE, GVA14.COD_ZONA
FROM GVA12 INNER JOIN GVA14 ON GVA12.COD_CLIENT = GVA14.COD_CLIENT
GROUP BY GVA12.FECHA_EMIS, GVA12.IMPORTE, GVA14.COD_ZONA
HAVING (((GVA12.FECHA_EMIS)=Month(curDate())));

它给出了一个空表。


@bluefeet这就是我想要的决赛桌

COD_ZONA            IMPORTE
1                   1457.56
16                  127.45

2 个答案:

答案 0 :(得分:1)

听起来您需要使用此功能,将Month()功能同时应用于GVA12.FECHA_EMIScurDate()

SELECT GVA12.FECHA_EMIS, GVA12.IMPORTE, GVA14.COD_ZONA
FROM GVA12 INNER JOIN GVA14 
  ON GVA12.COD_CLIENT = GVA14.COD_CLIENT
WHERE Month(GVA12.FECHA_EMIS)=Month(curDate())
GROUP BY GVA12.FECHA_EMIS, GVA12.IMPORTE, GVA14.COD_ZONA

如果你想比较月份和年份,那么我会使用:

SELECT GVA12.FECHA_EMIS, GVA12.IMPORTE, GVA14.COD_ZONA
FROM GVA12 INNER JOIN GVA14 
  ON GVA12.COD_CLIENT = GVA14.COD_CLIENT
WHERE Month(GVA12.FECHA_EMIS)=Month(curDate())
  AND Year(GVA12.FECHA_EMIS)=Year(curDate())
GROUP BY GVA12.FECHA_EMIS, GVA12.IMPORTE, GVA14.COD_ZONA

这将确保月份和年份相等。

如果您想要sum(),那么您可以使用:

SELECT GVA14.COD_ZONA, sum(GVA12.IMPORTE)
FROM GVA12 INNER JOIN GVA14 
  ON GVA12.COD_CLIENT = GVA14.COD_CLIENT
WHERE Month(GVA12.FECHA_EMIS)=Month(curDate())
  AND Year(GVA12.FECHA_EMIS)=Year(curDate())
GROUP BY GVA14.COD_ZONA

答案 1 :(得分:0)

GVA12.FECHA_EMIS存储月份或完整日期?您正在与当月比较。

HAVING (((GVA12.FECHA_EMIS)=Month(curDate())));