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 来自第一个查询是否有任何最佳方法可以在单个查询中调用这些查询,以便优化。

4

1 回答 1

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
于 2012-10-03T07:33:29.397 回答