Joining two MySQL queries

Discussion in 'MySQL' started by agd, Mar 23, 2011.

  1. #1
    Hi I'm new to MySQL and I would like to know if you can help me optimizing and joining this queries:

    so.. I have this code:
    $sqlDomain = mysql_query('
    						SELECT Id
    						FROM site
    						WHERE Status = 1
    						AND IdSiteCategories = 2
    						ORDER BY TotalRatio ASC 
    						LIMIT 10'
    						) or die("Error:&nbsp;<b>".mysql_error()."</b>.");
    
    
    while($rowDomain = mysql_fetch_assoc($sqlDomain)){
    
    	$sqlPost = mysql_query('
    						  SELECT Id,IdSite,Title 
    						  FROM post 
    						  WHERE Status = 1
    						  AND IdPostCategories IN (2,4,5)
    						  AND IdSite = '.$rowDomain['Id'].'
    						  ORDER BY Ratio DESC 
    						  LIMIT 5') or die("Error:&nbsp;<b>".mysql_error()."</b>."); 
    						  
    	while($rowPost = mysql_fetch_assoc($sqlPost)){
    		echo $rowPost['Id'];
    		echo $rowPost['Title'];
    		echo $rowPost['IdSite'];
    	}
    }
    PHP:
    I tried doing my query this way:

    $sqlPost = mysql_query('
    						SELECT site.Id, post.Id, post.IdSite, post.Title 
    						FROM site, post 
    						WHERE site.Status = 1 
    						AND site.IdSiteCategories = 5 
    						AND post.Status = 1
    						AND post.IdPostCategories IN (2,4,5)
    						AND post.IdSite = site.Id 
    						ORDER BY post.Ratio DESC 
    						LIMIT 5') or die("Error:&nbsp;<b>".mysql_error()."</b>.");
    				  
    while($rowPost = mysql_fetch_assoc($sqlPost)){
    	echo $rowPost['Id'];
    	echo $rowPost['Title'];
    	echo $rowPost['IdSite'];
    }
    
    PHP:
    The problem with this one is I can't limit or order the "site" table.
     
    agd, Mar 23, 2011 IP
  2. Martin K

    Martin K Active Member

    Messages:
    262
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Martin K, Apr 1, 2011 IP