I have this attendance table
---------------------------------------------------------------------------
attendance_id | stud_id | week | sy |sem |present
---------------------------------------------------------------------------
1 | 1 | 02/18/2012 | 2010-2011 |1st semester|1
2 | 2 | 02/18/2012 | 2010-2011 |1st semester|1
3 | 3 | 02/18/2012 | 2010-2011 |1st semester|1
4 | 1 | 02/25/2012 | 2010-2011 |1st semester|1
5 | 2 | 02/25/2012 | 2010-2011 |1st semester|1
6 | 1 | 03/03/2012 | 2010-2011 |1st semester|1
7 | 2 | 03/03/2012 | 2010-2011 |1st semester|1
8 | 3 | 03/03/2012 | 2010-2011 |1st semester|1
my query is this
Select cadet_record.fname,cadet_record.lname,cadet_record.mname, student_id,
MAX(case WHEN week = '02/18/2012' then present end) as 'week1',
MAX(case WHEN week = '02/25/2012' then present end) as 'week2'
From attendance
LEFT JOIN cadet_record ON cadet_record.stud_no = attendance.student_id WHERE section = '$section' AND schoolyear = '$year' AND component = '$component' AND semester = '$semester'
GROUP BY student_id
how can I dynamically call all the week without inserting the dates
for e.g. 02/28/2012, 02/29/2012 so on and so forth.
any ideas? =(
As far as I know, you can’t dynamically add columns to a SELECT statement. What you’re asking for is a way of presenting data and that is not something MySQL cares about. You should handle that in the front end.
However, you can cheat by creating your queries in your model and dynamically adding those new columns, by dynamically inserting more
MAX(case...to the query string. That’s not a nice solution, though.Edit:
So, I guess you’re talking about the ugly solution. Well, basically you should dynamically create your query string (pseudocode):
Then your query should look like this for dates from 02/18/2012 to 03/18/2012:
GROUP BY student_id
Notice I added days instead of weeks because your example showed increasing days, although the column name was weeks