0

I have a student attendance table in mysql 19BE1attendance. The columns are number, name, total, Week_2, Week_1

Week_X is either 1 or 0

Of course the number of weeks will increase as the term goes on.

The column total should be the sum of the Week_X values.

This does what I want, all the values are added nicely in the third column total:

SELECT Week_2, Week_1, (Week_1 + Week_2) as total FROM 19BE1attendance;

EDIT: I made php which can import a .csv into a table. That is how the attendance values will get into the attendance table.

Now I have total. Assume I have another table totals with 3 columns number name totals

How do I get total in the column totals for each student?

Or, alternatively, add the attendance values on the fly and output them in a webpage with a foreach() when a student wishes to consult his or her scores.

I can already output fictitious score values using php.

(number is the student number and is a unique identifier for each student)

4
  • 1
    Tables in relational databases aren't spreadsheets. They represent relations. You're using it wrong.
    – sticky bit
    Commented Jan 31, 2020 at 6:28
  • I see, that's my problem. I'm just starting with mysql
    – Pedroski
    Commented Jan 31, 2020 at 6:39
  • Presumably, there could never be a week_3 or week_0?
    – Strawberry
    Commented Jan 31, 2020 at 8:15
  • There are 18 weeks in all. I'm just experimenting. I tired this: SELECT number, Week_2, Week_1, (Week_2 + Week_1) AS total FROM 19BE1attendance WHERE number = '1925010101'; That gets me what I want. Just need to put that into php!
    – Pedroski
    Commented Jan 31, 2020 at 9:06

2 Answers 2

3

You should really consider changing your table design to this:

name | week_no | total

The week_no column optionally could also be a date or a timestamp, from which you could easily extract the week number. With this design, if you want totals for each name, you need only use:

SELECT name, SUM(total) AS total
FROM yourTable
GROUP BY name;

The problem with your current design is that adding weeks as new columns does not scale well at all. In addition, you should probably avoid storing the grand total as a separate column, as it is derived data. Should the underlying totals change over time, your suggested grand total column could become out of date, requiring frequent updates.

2
  • I need the number in there, the student number. It is a unique identifier. Later, I've got to get php to do this, but first I want to know how to do it in bash.
    – Pedroski
    Commented Jan 31, 2020 at 6:40
  • Then edit your quesiton and show us the actual problem. I have answered as best I could, given the information you did provide us. Commented Jan 31, 2020 at 6:50
2

I'm affraid there is no easy solution for your problem, because the layout of the table is designed poorly. I would strongly suggest to refactor this table if you can. A more appropiate layout would be something like:

CREATE TABLE attendance (
 id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Entry identifier',
 name VARCHAR NOT NULL COMMENT 'The persons name',
 year INT UNSIGNED NOT NULL COMMENT 'Year of week in which attendance is noted',
 week     INT UNSIGNED NOT NULL COMMENT 'Weeknumber for which attendance is noted',
 is_present TINYINT UNSIGNED NOT NULL COMMENT 'Whether person was present (1) or not (0)'
) ENGINE Innodb;

Using this layout you could make a very simple SELECT statement getting the overall attendance stats per person.

4
  • Actually, I need the week-by-week values to present to my boss at the end of term. Of course, I have them anyway, they are in my spreadsheet. I would upload them as .csv. What I am working on is a php driven webpage which gets the attendance, test scores and totals for each student and displays them on a webpage. The student number is a unique identifier. The student enters his or her number and can see at a glance all his or her data.
    – Pedroski
    Commented Jan 31, 2020 at 6:44
  • So, for now, I need to get total from SELECT Week_2, Week_1, (Week_1 + Week_2) as total FROM 19BE1attendance; and put it in my php output.
    – Pedroski
    Commented Jan 31, 2020 at 6:47
  • Maybe I could make another table, totals, with number, name and totals, then make totals equal to total for each student? Would that be a way to go?
    – Pedroski
    Commented Jan 31, 2020 at 6:51
  • I think this table lay-out provide what you need. Each week "attendance" is a single record, you can do a SUM on the is_present column and GROUP by on the number which will give you the total of "attandance" per student optionally add a WHERE to get the total a specific person or persons. Commented Jan 31, 2020 at 18:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.