| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| NamePros Regular | Query Optimization I am GOING CRAZY over this query. I can't figure it out. What I want: User logs in, we need to grab all courses and the status of that user in relation to a particular class (if available). Format: A list of school courses and each course may or may not have a status for the user associated to it. Table expected: Code: classId | [status] --------------------- 13 | taken 14 | 15 | ip These are some queries I was trying: Code: SELECT c.classId, sc.status FROM class AS c LEFT OUTER JOIN scheduleClasses AS sc, user AS u, schedule AS s ON sc.scheduleId = '21' && sc.scheduleId = s.scheduleId && u.userId = '11' && c.classId = sc.classId && ( c.qtr = '1' || c.qtr = '2' || c.qtr = '3' || c.qtr = '4' ) ORDER BY ordinal LIMIT 0 , 30 HTML Code: SELECT c.classId, sc.status FROM class AS c INNER JOIN scheduleClasses AS sc, user AS u, schedule AS s ON sc.scheduleId = '21' && sc.scheduleId = s.scheduleId && u.userId = '11' && c.classId = sc.classId && ( c.qtr = '1' || c.qtr = '2' || c.qtr = '3' || c.qtr = '4' ) ORDER BY ordinal LIMIT 0 , 30 Table setup: Code: -- -- Table structure for table `class` -- CREATE TABLE IF NOT EXISTS `class` ( `classId` int(3) NOT NULL auto_increment, `course` varchar(255) NOT NULL default '', `title` varchar(255) NOT NULL default '', `descrip` text NOT NULL, `prereqs` varchar(255) NOT NULL default '', `qtr` int(2) NOT NULL default '0', `ordinal` tinyint(4) NOT NULL default '0', PRIMARY KEY (`classId`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=62 ; -- -------------------------------------------------------- -- -- Table structure for table `schedule` -- CREATE TABLE IF NOT EXISTS `schedule` ( `scheduleId` int(5) NOT NULL auto_increment, `userId` int(5) NOT NULL default '0', `hashId` int(10) NOT NULL default '0', PRIMARY KEY (`scheduleId`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=82 ; -- -------------------------------------------------------- -- -- Table structure for table `scheduleClasses` -- CREATE TABLE IF NOT EXISTS `scheduleClasses` ( `scheduleId` int(5) NOT NULL default '0', `classId` int(5) NOT NULL default '0', `status` varchar(5) NOT NULL default '', `grade` char(1) NOT NULL default '', `notes` text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; How I'm currently doing it: PHP Code: Let me know if you need more info. Last edited by DylanButler; 03-08-2008 at 11:47 PM. |
| |
| | #2 (permalink) |
| NPQ's PA, Slave, and On Call Coder Technical Services | At first glance; PHP Code:
__________________ |
| |
| | #3 (permalink) |
| NamePros Regular | scheduleId is set previously through a cookie. Your version threw up errors because of the line with the parenthesis: ( ON c.classId = sc.classId ) So I changed it to this, which returns not enough listings. I should note the scheduleClasses table may or may not have an entry for each class, but it will never have two entries for the same class and schedule. Code: SELECT c.classId, c.title, sc.status FROM class AS c LEFT JOIN scheduleClasses AS sc ON ( c.classId = sc.classId ) WHERE c.qtr IN ( 1, 2, 3, 4 ) AND sc.scheduleId = '21' ORDER BY c.ordinal LIMIT 0 , 30 Edit: could this help be adapted for this situation? http://blogs.x2line.com/al/articles/184.aspx Last edited by DylanButler; 03-09-2008 at 02:46 PM. |
| |
| | #5 (permalink) |
| NamePros Regular | FIGURED IT OUT! Code: SELECT c.classId, c.title, sc.status
FROM class AS c
LEFT JOIN scheduleClasses AS sc ON (c.classId = sc.classId AND sc.scheduleId = 21 )
WHERE c.qtr IN(1,2,3,4)
ORDER BY c.ordinal
Last edited by DylanButler; 03-10-2008 at 08:56 PM. |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |