SQL query help

Discussion in 'Software' started by msteinblock, Nov 14, 2005.

  1. msteinblock

    msteinblock Private E-2

    I have the follow script written, which is VERY inefficient. The page times out before it is done. I am pretty sure there is a way to combine both queries into one, but I am not sure how. Anyone help me?

    $Query = "SELECT RosterID FROM Roster";
    $Result = mysql_query($Query);
    while ($myrow = mysql_fetch_array($Result))
    {
    $RosterID=$myrow["RosterID"];
    $Query2 = "SELECT RosterID FROM Photos WHERE RosterID = $RosterID";
    $Result2 = mysql_query($Query2);
    $num = mysql_num_rows($Result2);
    if (!$num)
    {
    print $RosterID . "<BR>";
    }
    }
     
  2. Kodo

    Kodo SNATCHSQUATCH

    Select Photos.RosterID from Photos INNER JOIN Roster ON Photos.RosterID=Roster.RosterID where Photos.RosterID=$RosterID

    Try that..
     
  3. msteinblock

    msteinblock Private E-2

    Where are you coming up with $RosterID?

    What I am trying to do it basically find all of the Roster.RosterID's that do not have a Photo.RosterID associated with it. In my initial query, I first did a search for all Roster.RosterID's, then did another query against that result to find how many returned 0 results. I know how to do inner joins...just not sure how list the results I am looking for.

    Thanks!
     
  4. Kodo

    Kodo SNATCHSQUATCH

    SELECT Roster.RosterID
    FROM Photos RIGHT JOIN Roster ON Photos.RosterID = Roster.RosterID
    WHERE (((Photos.RosterID) Is Null));
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds