在mysql中将初始值设置为零

时间:2016-09-09 14:10:54

标签: mysql math set operation

我有一个查询可以满足我的需求,但由于图表的joomla插件不接受初始SET值为零,我需要一种方法将此查询转换为视图,或者至少执行算术函数不使用SET。

基本上,此查询显示每个月的列,其中包含员工总数,添加上个月的列并减去剩余的人员。

SELECT
  CONCAT(q1.d,'-',LPAD(q1.m,2,0)) AS Periodo,
q1.c AS `<font color="blue">Assunzioni</font>`,
q1.l AS `<font color="red">Licenziamenti</font>`
FROM
 (SELECT
   YEAR(calendar.datefield) AS d,
   month(calendar.datefield) AS m,
   SUM(statistica_assunti_n.assunzioni) AS c,
   SUM(statistica_licenziati_n.licenziamenti) AS l
FROM  calendar
INNER JOIN statistica_assunti_n
ON calendar.datefield = statistica_assunti_n.data_assunzioni
INNER JOIN statistica_licenziati_n
ON calendar.datefield = statistica_licenziati_n.data_licenziamenti
WHERE YEAR(datefield) ={$REQUEST:chartYearStart|empty:'2011'} 
AND datefield       <= DATE(NOW())
GROUP  BY d, m
ORDER  BY d, m) AS q1 

任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:1)

插件的管理员给了我解决方案,(SELECT @runtot:= 0)AS为零:

SELECT
    CONCAT(q1.d,'-',LPAD(q1.m,2,0)) AS Periodo,
   (@runtot := @runtot + q1.c - q1.l) AS `N. dipendenti`
FROM
   (SELECT
       YEAR(calendar.datefield) AS d,
       month(calendar.datefield) AS m,
       SUM(statistica_assunti_n.assunzioni) AS c,
       SUM(statistica_licenziati_n.licenziamenti) AS l
    FROM  calendar
    INNER JOIN statistica_assunti_n
    ON calendar.datefield = statistica_assunti_n.data_assunzioni
  INNER JOIN statistica_licenziati_n
    ON calendar.datefield = statistica_licenziati_n.data_licenziamenti
WHERE YEAR(datefield) = {$REQUEST:chartYearStart|empty:'2011'} AND datefield <= DATE(NOW())
    GROUP  BY d, m
    ORDER  BY d, m) AS q1, (SELECT @runtot := 0) AS zero