i've created a employee attendance table having 3 columns emp_name, emp_present & date,
there is no primary key in mytable
i'm using this query to get total presents of employees
SELECT `emp_name`, `emp_present`,(select sum(emp_present='present')
from empattendance) as 'total presents', `date` FROM `empattendance` where date=curdate()
but i want to get total presents and 'total absents' of each indiviual shown with them in total presents and 'total absents' column respectively. I'm a newbie in database and don't have much knowledge about how to sort this problem.....
答案 0 :(得分:0)
You can use conditional aggregation, to get the count of rows where employee was present and the count of rows where employee was absent like this:
SELECT emp_name,
SUM(emp_present = 'present') AS numPresence,
SUM(emp_present = 'absent') AS numAbsence
FROM employees
GROUP BY emp_name;
EDIT:
If you want a running total of presence and absences, you'll need to use a variable:
SET @present := 0;
SET @absent := 0;
SELECT emp_name, dateColumn,
IF(emp_present = 'present', @present := @present + 1, @present) AS numPresence
IF(emp_present = 'absent', @absent := @absent+ 1, @absent) AS numAbsense
FROM myTable
ORDER BY dateColumn;