NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Query Optimizationg

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 03-09-2008, 12:30 AM THREAD STARTER               #1 (permalink)
NamePros Regular
 
DylanButler's Avatar
Join Date: Jan 2006
Location: San Diego, CA
Posts: 735
DylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to behold
 



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
(continued for a list of all classes (unique), notice status is optional)

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
(Returns too many rows)

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
(This does what I want, only it doesn't return enough rows.) It should return a list of ALL classes, and if that user has a status set for a particular class, then return that too.

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;

????: NamePros.com http://www.namepros.com/programming/442196-query-optimizationg.html
????: NamePros.com http://www.namepros.com/showthread.php?t=442196
How I'm currently doing it:

PHP Code:
<?
$sql 
mysql_query("select classId, title from class where qtr = '1' || qtr = '2' || qtr = '3' || qtr = '4' order by ordinal");
while (
$row mysql_fetch_array($sql)) {    
    
$classid $row['classId'];
    
$innersql mysql_query("select status from scheduleClasses where scheduleId = '$scheduleId' and classId = '$classid'");
    
$innerrow mysql_fetch_array($innersql);
    
$status $innerrow[0]; 
    echo 
"<div class=\"class $status\" id=\"class-".$row['classId']."\">
        <a href=\"#\" class=\"name\">"
.$row['title']."</a>
        <a href=\"#nogo\" class=\"open\"><span>Open</span></a>
        <div class=\"options\">
            <a href=\"#\" class=\"taken\">Taken</a>
            <a href=\"#\" class=\"ip\">In progress</a>
            <a href=\"#\" class=\"not\">Not taken</a>
        </div>
    </div>"
;
}
?>
As you can see, we are sending a query within each iteration of the loop, which I dont think should be necessary here.
Let me know if you need more info.
Last edited by DylanButler; 03-09-2008 at 12:47 AM.
DylanButler is offline  
Old 03-09-2008, 10:22 AM   #2 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
At first glance;

PHP Code:
<?php

// How is $scheduleId determined?

$sql mysql_query("
    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 = '
$scheduleId'
    ORDER BY c.ordinal
"
) or die(mysql_error());

while (
$row mysql_fetch_array($sql))
{
    echo 
"
    <div class=\"class 
$row[status]\" id=\"class-$row[classId]\">
        <a href=\"#\" class=\"name\">
$row[title]</a>
        <a href=\"#nogo\" class=\"open\"><span>Open</span></a>
        <div class=\"options\">
            <a href=\"#\" class=\"taken\">Taken</a>
????: NamePros.com http://www.namepros.com/showthread.php?t=442196
            <a href=\"#\" class=\"ip\">In progress</a>
            <a href=\"#\" class=\"not\">Not taken</a>
        </div>
    </div>
"
;
}

?>
Eric is offline  
Old 03-09-2008, 03:20 PM THREAD STARTER               #3 (permalink)
NamePros Regular
 
DylanButler's Avatar
Join Date: Jan 2006
Location: San Diego, CA
Posts: 735
DylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to behold
 



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

????: NamePros.com http://www.namepros.com/showthread.php?t=442196
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 03:46 PM.
DylanButler is offline  
Old 03-09-2008, 09:37 PM   #4 (permalink)
NamePros Regular
 
monaco's Avatar
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 689
monaco will become famous soon enough
 



Try a right outer join ?.?
__________________
My Website | My Blog
monaco is offline  
Old 03-10-2008, 09:38 PM THREAD STARTER               #5 (permalink)
NamePros Regular
 
DylanButler's Avatar
Join Date: Jan 2006
Location: San Diego, CA
Posts: 735
DylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to behold
 



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
Thanks guys
Last edited by DylanButler; 03-10-2008 at 09:56 PM.
DylanButler is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 07:24 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger