Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7534317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:58:52+00:00 2026-05-30T05:58:52+00:00

I have this attendance table ————————————————————————— attendance_id | stud_id | week | sy |sem

  • 0

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? =(

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:58:53+00:00Added an answer on May 30, 2026 at 5:58 am

    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:

    Im using php, how can I accomplish that?

    So, I guess you’re talking about the ugly solution. Well, basically you should dynamically create your query string (pseudocode):

    $initialDay = 02/28/2012;
    $lastDay = 03/28/2012;
    $dayNumber = 1;
    $sql = 'Select cadet_record.fname,cadet_record.lname,cadet_record.mname, student_id';
    while ($initialDay <= $lastDay) {
        $sql .= ', MAX(case WHEN week = $initialDay then present end) as day' . $dayNumber;
        $initialDay = $initialDay + 1 day;
        $dayNumber++;
    }
    $sql .= ' From attendance blah blah...';
    

    Then your query should look like this for dates from 02/18/2012 to 03/18/2012:

    Select cadet_record.fname,cadet_record.lname,cadet_record.mname, student_id,
    ,MAX(case WHEN week = '02/18/2012' then present end) as day1
    ,MAX(case WHEN week = '02/19/2012' then present end) as day2
    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

    Notice I added days instead of weeks because your example showed increasing days, although the column name was weeks

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table named 'Attendance' which is used to record student attendance time
I have a table that tracks attendance in a course. The columns are the
Possible Duplicate: MySQL dynamic cross tab I have student_record table ------------------------------------------------------------------- student_id | semester
So I have a SQL table that contains time-clock information like this. The number
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=
I have this setup where in my development copy I can commit changes on
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.