mysql
  • nested-queries
  • 2012-10-03 86 views 0 likes 
    0
    $image=mysql_query("select * from carad order by carAdID DESC LIMIT 10"); 
    
        while($row=mysql_fetch_array($image)) 
        { 
          $getimage=mysql_query("select * from carimage where carAdID='".$row["carAdID"]."' LIMIT 1"); 
          while($getimagerow=mysql_fetch_array($getimage)) 
          { 
           echo(''); 
           echo('<a href="#" ><div class="simages"><img src="'.substr($getimagerow["carImageUrl"],1).'" width="600" height="400" /><div class="SliderContent">'.$row["catalogPrice"].'</div></div></a>'); 
          } 
        } 
    

    我使用兩個查詢一個是前while循環和while循環的ID是來自於第一個查詢有沒有打電話給這些查詢在一個單一的查詢,使其優化任何最好的方法。嵌套的MySQL查詢到單個查詢

    回答

    0

    試試這個:

    SELECT ci.* 
    FROM carimage ci 
    INNER JOIN 
    (
        select * 
        from carad 
        order by carAdID DESC LIMIT 10 
    ) c ON ci.carAdID = c.carAdID 
    --May be you need to add LIMIT here as well 
    
    相關問題