Jump to content

Help with simple select


kaylee

Recommended Posts

Hi everyone,

 

I am new to mysql and databases in general.

 

I have an "attendance" table with the following information:

date, school, class, and studentid.  I would like to list the number of

students for each class, for each school, for each day even if the attendance was zero.

 

If I had 2 schools(s1, s2) and 3 classes (c1,c2,c3), what is the best way to get an output such as:

 

--------------------------------

day - school - class - students

--------------------------------

1 - s1 - c1 - 5

1 - s1 - c2 - 0

1 - s1 - c3 - 1

1 - s2 - c1 - 1

1 - s2 - c2 - 2

1 - s2 - c3 - 0

 

2 - s1 - c1 - 0

2 - s1 - c2 - 3

2 - s1 - c3 - 4

2 - s2 - c1 - 20

2 - s2 - c2 - 23

2 - s2 - c3 - 31

 

3 - s1 - c1 - 9

3 - s1 - c2 - 2

3 - s1 - c3 - 3

3 - s2 - c1 - 0

3 - s2 - c2 - 32

3 - s2 - c3 - 23

..

..

..

 

I tried "SELECT datetime, school, class, COUNT(studentid) as students FROM attendance GROUP BY datetime, school, class ORDER BY datetime, school, class;"

 

But this will not return rows with empty student count

 

--------------------------------

day - school - class - students

--------------------------------

1 - s1 - c1 - 5

1 - s1 - c3 - 1

1 - s2 - c1 - 1

1 - s2 - c2 - 2

 

 

2 - s1 - c2 - 3

2 - s1 - c3 - 4

2 - s2 - c1 - 20

2 - s2 - c2 - 23

2 - s2 - c3 - 31

 

3 - s1 - c1 - 9

3 - s1 - c2 - 2

3 - s1 - c3 - 3

3 - s2 - c2 - 32

3 - s2 - c3 - 23

 

 

should I be looking into JOINS here?

Thanks in advance,

K

Link to comment
Share on other sites

I'm sure theres a better way to do this

 

though my first impression would be a couple nested for loops

 

<?php
$schools = [number of schools];
$days = [number of days];
$class = [number of classes];

for ($d=0; $d <= $days; $d++) {

for ($s=0; $s <= $schools; $s++) {

	for ($c=0; $c <= $class; $c++) {

		$query = "SELECT count(studentid) AS students FROM attendance
			WHERE day = " .$d. "
			AND school = " .$s. "
			AND class = " .$c;
		$result = mysql_query($query);

		print $d . " - s" . $s . " - c" . $c . " - " . mysql_result($result, 0, 0) . "<br />";

	}

}

}

?>

 

syntax might be off as I typed this in the browser but gives one approach that will work

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.