Gaoshan Posted September 25, 2006 Share Posted September 25, 2006 Hi all, my problem is this:I want to be able to list students and then list their grades for each assignment after them (in a traditional gradebook pattern).I only want the students listed once and some students will not yet have been graded on all assignments. Also, not all assignments will have been created. I suppose I could do the work in PHP but it seems this MUST be doable in SQL. Any help is greatly appreciated (I'm a teacher, BTW)I can get a list that restricts the students by student id and lists their first assignment but my problem is getting second, third, etc. assignments to go after that (it is easy to get them to appear UNDER that but then it lists the student's name again). I would assume the main issue here is how to extract the different assignment numbers from the single column that they share. ??? >:( What I get:[table][tr][td]John[/td][td]9[/td][/tr][tr][td]Mary[/td][td]7[/td][/tr][tr][td]Ann[/td][td]8[/td][/tr][tr][td]John[/td][td]5[/td][/tr][tr][td]Mary[/td][td]4[/td][/tr][tr][td]Ann[/td][td]9[/td][/tr][tr][td]John[/td][td]6[/td][/tr][/table] ;D What I want:[table][tr][td]John[/td][td]9[/td][td]5[/td][td]6[/td][/tr][tr][td]Mary[/td][td]7[/td][td]4[/td][td]-[/td][/tr][tr][td]Ann[/td][td]8[/td][td]9[/td][td]-[/td][/tr][/table]Here is an overview of the database as it stands now:-- Table structure for table `assignmentdata`-- TABLE `assignmentdata` ( `id` smallint(6) NOT NULL auto_increment, `assignmentnum` tinyint(20) NOT NULL, `assignmentname` varchar(100) NOT NULL, `maxgradepoints` tinyint(100) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;-- ---------------------------------------------------------- -- Table structure for table `grades`-- TABLE `grades` ( `id` smallint(6) NOT NULL auto_increment, `studentid` smallint(6) NOT NULL, `assignmentid` smallint(6) NOT NULL, `earned` decimal(3,1) NOT NULL, `comment` text NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;-- ---------------------------------------------------------- -- Table structure for table `studentdata`-- TABLE `studentdata` ( `id` smallint(6) NOT NULL auto_increment, `studentid` smallint(6) NOT NULL, `firstname` varchar(30) NOT NULL, `lastname` varchar(30) NOT NULL, `attendance` varchar(10) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1; Quote Link to comment Share on other sites More sharing options...
fenway Posted September 25, 2006 Share Posted September 25, 2006 Well, depending on the output, you could probably cheat with a GROUP_CONCAT(). Quote Link to comment Share on other sites More sharing options...
Gaoshan Posted September 26, 2006 Author Share Posted September 26, 2006 OK. I was not familiar with GROUP_CONCAT. I used the following query:[code]SELECT s.firstname, GROUP_CONCAT( g.earned ) AS groupedFROM studentdata s, grades g, assignmentdata aWHERE g.assignmentid = a.assignmentnumAND s.studentid = g.studentidGROUP BY s.studentid[/code]and got:[table][tr][td]Jim[/td][td]9,9.1[/td][/tr][tr][td]Mary[/td][td]8[/td][/tr][tr][td]Ann[/td][td]6[/td][/tr][/table]Which is better than I had been able to get before. I'll play with that and see if it solves the problem. Thanks for the tip! Quote Link to comment Share on other sites More sharing options...
fenway Posted September 26, 2006 Share Posted September 26, 2006 You can have an ORDER BY in your GROUP_CONCAT() as well, and choose the separator. Quote Link to comment Share on other sites More sharing options...
Gaoshan Posted September 26, 2006 Author Share Posted September 26, 2006 Yes. I figured I could use the default comma separator and then use explode in PHP to get the various values. I just wish I knew how to do everything with one SQL statement here rather than resort to doing part of it in PHP. Quote Link to comment Share on other sites More sharing options...
fenway Posted September 26, 2006 Share Posted September 26, 2006 Well, you can use a space, and an IFNULL() statement to replace NULLs with '-' as desired. Quote Link to comment Share on other sites More sharing options...
Gaoshan Posted September 26, 2006 Author Share Posted September 26, 2006 True. I'll see what works best with my setup and give that a try. Quote Link to comment Share on other sites More sharing options...
Gaoshan Posted September 28, 2006 Author Share Posted September 28, 2006 My solution to this problem was as follows:First the sql:[code]$mysqldb->query("SELECT s.firstname, COUNT(DISTINCT g.assignmentid) AS totalstu, GROUP_CONCAT( g.earned ) AS groupedFROM studentdata s, grades g, assignmentdata aWHERE g.assignmentid = a.assignmentnumAND s.studentid = g.studentidGROUP BY s.studentid");[/code]And now the PHP to loop through the results and put what I want where I want it:[code]while ($row = $mysqldb->fetchObject()) { echo '<tr><td>' . $row->firstname . '</td>'; for ($i = 0; $i < $row->totalstu; $i++){ $bustedout = explode(',', $row->grouped); echo '<td>' . $bustedout[$i] . '</td>'; } echo '</tr>'; }[/code]Thanks to fenway for pointing out the piece of the puzzle that I was missing... GROUP_CONCAT Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.