1

how can I count Mysql Field Rows?

For example I have table called student_attendance where we have 4 fields: absent, present, holiday, leave.

There is list of 10+ students, each student value will go to its own field, like if for one student we selected absent, 1 value will go to absent field and if someone select present, 1 value will go to present field.

Now what i want is

Each time attendance take, new row insert in student_attendance table for the student.

So if there are 10 Rows in student_attendance table, how can i + all of them

Like if there are 10 rows of present field, 3 rows are empty and 7 rows have 1 value, how can i count it so that the total value of specific field 1+1+1+1+1+1+1 can comes in php so it show 7 ?

1
  • What have you tried? What hasn't worked? What research have you done to solve this problem? The thing you're looking for is called an aggregate function. Did you know this term before? If not, go look it up. Commented Dec 28, 2012 at 10:24

3 Answers 3

1
    SELECT SUM(present) AS presence_days
    FROM student_attendance

This will result:

+---------------+
| presence_days |
+---------------+
| 7             |
|               |
+---------------+

Example SQL (Demo):

Sign up to request clarification or add additional context in comments.

1 Comment

@hakre Thank you for your addition, it is really useful especially the Demo
0
select count(*) from table_name where present_field=1;

Comments

0

Try this:

SELECT SUM(absent), SUM(present), SUM(holiday), SUM(leave) 
FROM student_attendance 
GROUP BY sudentId;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.