我试图获得每年/每月的出勤率类型以填充条形图。 这是我的两张桌子(我知道英语有点偏差):
import java.util.ArrayList;
import main.ClassPessoa;
import main.ClassCliente;
import main.ClassFornecedor;
import main.ClassStock;
import main.ClassVenda;
import main.ClassRecurso;
import java.io.Serializable;
/**
*
* @author Skray
*/
public class ClassListas implements Serializable {
//Listas
public static ArrayList<ClassPessoa> ListaPessoa = new ArrayList<ClassPessoa>();
public static ArrayList<ClassCliente> ListaCliente = new ArrayList<ClassCliente>();
public static ArrayList<ClassFornecedor> ListaFornecedor = new ArrayList<ClassFornecedor>();
public static ArrayList<ClassStock> ListaStocks = new ArrayList<ClassStock>();
public static ArrayList<ClassVenda> ListaVenda = new ArrayList<ClassVenda>();
public static ArrayList<ClassRecurso> ListaRecurso = new ArrayList<ClassRecurso>();
}
这是我当前的查询,它返回所有结果而不是预期结果,这只是当前年份:
**presences**
ID | Id_student | Id_class | Presence
3 2 27 1
4 4 28 2
... ... ... ...
**class**
ID | Id_schedule | Date_class | State
27 6 2017-12-11 1
28 7 2018-01-13 1
... ... ... ...
这是结果,也显示了去年的事件:
SELECT
DATE_FORMAT(Date_class, '%Y') as 'year', DATE_FORMAT(Date_class, '%M') as 'month',
(SELECT COUNT(distinct ID) FROM presences WHERE Presence = 0) as Faltas,
(SELECT COUNT(distinct ID) FROM presences WHERE Presence = 1) as Pre,
(SELECT COUNT(distinct ID) FROM presences WHERE Presence = 2) as Temp,
(SELECT COUNT(distinct ID) FROM presences WHERE Presence = 3) as Canc,
(SELECT COUNT(distinct ID) FROM presences WHERE Presence = 4) as FaltasTemp
FROM class, presences
WHERE YEAR(Date_class) = YEAR(CURDATE())
AND
presences.Id_class = class.ID
GROUP BY DATE_FORMAT(Date_class, '%Y%m')
有人能帮我辨别失踪的东西吗?
答案 0 :(得分:2)
我认为你想要一个更简单的查询:
SELECT YEAR(c.Date_class) as year, MONTH(c.Date_class) as month,
SUM(p.Presence = 0) as Faltas,
SUM(p.Presence = 1) as Pre,
SUM(p.Presence = 2) as Temp,
SUM(p.Presence = 3) as Canc,
SUM(p.Presence = 4) as FaltasTemp
FROM class c JOIN
presences p
ON p.Id_class = c.ID
WHERE YEAR(c.Date_class) = YEAR(CURDATE())
GROUP BY YEAR(c.Date_class), MONTH(c.Date_class);
注意:
JOIN
语法。 从不在FROM
子句中使用逗号。